Code review comment: Can we move these operations to the outside of the database lock?

Code under review:
private boolean syncTableMetaInternal(Database db, OlapTable table, boolean forceDeleteData) throws DdlException {
        StarOSAgent starOSAgent = GlobalStateMgr.getCurrentStarOSAgent();
        HashMap<Long, List<Long>> redundantGroupToShards = new HashMap<>();
        Locker locker = new Locker();
        locker.lockDatabase(db, LockType.READ);
        try {
            if (db.getTable(table.getId()) == null) {
                return false; 
            }
            if (table.getState() != OlapTable.OlapTableState.NORMAL) {
                return false; 
            }

            for (Partition partition : table.getAllPartitions()) {
                for (PhysicalPartition physicalPartition : partition.getSubPartitions()) {
                    long groupId = physicalPartition.getShardGroupId();
                    List<Long> feShardIds = new ArrayList<>();
                    for (MaterializedIndex materializedIndex :
                            physicalPartition.getMaterializedIndices(MaterializedIndex.IndexExtState.ALL)) {
                        for (Tablet tablet : materializedIndex.getTablets()) {
                            feShardIds.add(tablet.getId());
                        }
                    }
                    
                    List<Long> starmgrShardIds = starOSAgent.listShard(groupId);
                    starmgrShardIds.removeAll(feShardIds);
                    redundantGroupToShards.put(groupId, starmgrShardIds);
                }
            }
        } finally {
            locker.unLockDatabase(db, LockType.READ);
        }

        
        Set<Long> shardToDelete = new HashSet<>();
        for (Map.Entry<Long, List<Long>> entry : redundantGroupToShards.entrySet()) {
            if (forceDeleteData) {
                try {
                    dropTabletAndDeleteShard(entry.getValue(), starOSAgent);
                } catch (Exception e) {
                    
                    LOG.info(e.getMessage());
                }
            }
            shardToDelete.addAll(entry.getValue());
        }

        
        if (!shardToDelete.isEmpty()) {
            starOSAgent.deleteShards(shardToDelete);
        }
        return !shardToDelete.isEmpty();
    }

Line commented on: starmgrShardIds.removeAll(feShardIds);

Revised code:
private boolean syncTableMetaInternal(Database db, OlapTable table, boolean forceDeleteData) throws DdlException {
        StarOSAgent starOSAgent = GlobalStateMgr.getCurrentStarOSAgent();
        HashMap<Long, Set<Long>> redundantGroupToShards = new HashMap<>();
        List<PhysicalPartition> physicalPartitions = new ArrayList<>();
        Locker locker = new Locker();
        locker.lockDatabase(db, LockType.READ);
        try {
            if (db.getTable(table.getId()) == null) {
                return false; 
            }
            GlobalStateMgr.getCurrentState()
                    .getAllPartitionsIncludeRecycleBin(table)
                    .stream()
                    .map(Partition::getSubPartitions)
                    .forEach(physicalPartitions::addAll);
        } finally {
            locker.unLockDatabase(db, LockType.READ);
        }

        for (PhysicalPartition physicalPartition : physicalPartitions) {
            locker.lockDatabase(db, LockType.READ);
            try {
                if (table.getState() != OlapTable.OlapTableState.NORMAL) {
                    return false; 
                }
                
                long groupId = physicalPartition.getShardGroupId();
                List<Long> starmgrShardIds = starOSAgent.listShard(groupId);
                Set<Long> starmgrShardIdsSet = new HashSet<>(starmgrShardIds);
                for (MaterializedIndex materializedIndex :
                        physicalPartition.getMaterializedIndices(MaterializedIndex.IndexExtState.ALL)) {
                    for (Tablet tablet : materializedIndex.getTablets()) {
                        starmgrShardIdsSet.remove(tablet.getId());
                    }
                }
                
                redundantGroupToShards.put(groupId, starmgrShardIdsSet);
            } finally {
                locker.unLockDatabase(db, LockType.READ);
            }
        }

        
        Set<Long> shardToDelete = new HashSet<>();
        for (Map.Entry<Long, Set<Long>> entry : redundantGroupToShards.entrySet()) {
            if (forceDeleteData) {
                try {
                    List<Long> shardIds = new ArrayList<>();
                    shardIds.addAll(entry.getValue());
                    dropTabletAndDeleteShard(shardIds, starOSAgent);
                } catch (Exception e) {
                    
                    LOG.info(e.getMessage());
                }
            }
            shardToDelete.addAll(entry.getValue());
        }

        
        if (!shardToDelete.isEmpty()) {
            starOSAgent.deleteShards(shardToDelete);
        }
        return !shardToDelete.isEmpty();
    }