Code review comment: would rather keep the comment and delete the commented code.

Code before review:
protected void runPendingJob() throws AlterCancelException {
long numTablets = 0;
AgentBatchTask batchTask = new AgentBatchTask();
MarkedCountDownLatch<Long, Long> countDownLatch;
try (ReadLockedDatabase db = getReadLockedDatabase(dbId)) {
LakeTable table = getTableOrThrow(db, tableId);
Preconditions.checkState(table.getState() == OlapTable.OlapTableState.SCHEMA_CHANGE);
MaterializedIndexMeta indexMeta = table.getIndexMetaByIndexId(table.getBaseIndexId());
numTablets = partitionIndexMap.values().stream().map(MaterializedIndex::getTablets).mapToLong(List::size).sum();
countDownLatch = new MarkedCountDownLatch<>((int) numTablets);
for (long partitionId : partitionIndexMap.rowKeySet()) {
Partition partition = table.getPartition(partitionId);
Preconditions.checkState(partition != null);
TStorageMedium storageMedium = table.getPartitionInfo().getDataProperty(partitionId).getStorageMedium();
Map<Long, MaterializedIndex> shadowIndexMap = partitionIndexMap.row(partitionId);
for (Map.Entry<Long, MaterializedIndex> entry : shadowIndexMap.entrySet()) {
long shadowIdxId = entry.getKey();
MaterializedIndex shadowIdx = entry.getValue();
short shadowShortKeyColumnCount = indexShortKeyMap.get(shadowIdxId);
List<Column> shadowSchema = indexSchemaMap.get(shadowIdxId);
long originIndexId = indexIdMap.get(shadowIdxId);
KeysType originKeysType = table.getKeysTypeByIndexId(originIndexId);
List<Column> originSchema = table.getSchemaByIndexId(originIndexId);
List<Column> copiedShadowSchema = Lists.newArrayList();
for (Column column : shadowSchema) {
Column.DefaultValueType defaultValueType = column.getDefaultValueType();
if (defaultValueType == Column.DefaultValueType.CONST) {
Column copiedColumn = new Column(column);
copiedColumn.setDefaultValue(column.calculatedDefaultValueWithTime(startTime));
copiedShadowSchema.add(copiedColumn);
} else {
copiedShadowSchema.add(column);
}
}
List<Integer> copiedSortKeyIdxes = indexMeta.getSortKeyIdxes();
if (indexMeta.getSortKeyIdxes() != null) {
if (originSchema.size() > shadowSchema.size()) {
List<Column> differences = originSchema.stream().filter(element ->
!shadowSchema.contains(element)).collect(Collectors.toList());
Integer dropIdx = new Integer(originSchema.indexOf(differences.get(0)));
for (int i = 0; i < copiedSortKeyIdxes.size(); ++i) {
Integer sortKeyIdx = copiedSortKeyIdxes.get(i);
if (dropIdx < sortKeyIdx) {
copiedSortKeyIdxes.set(i, sortKeyIdx - 1);
}
}
} else if (originSchema.size() < shadowSchema.size()) {
List<Column> differences = shadowSchema.stream().filter(element ->
!originSchema.contains(element)).collect(Collectors.toList());
for (Column difference : differences) {
int addColumnIdx = shadowSchema.indexOf(difference);
for (int i = 0; i < copiedSortKeyIdxes.size(); ++i) {
Integer sortKeyIdx = copiedSortKeyIdxes.get(i);
int shadowSortKeyIdx = shadowSchema.indexOf(originSchema.get(
indexMeta.getSortKeyIdxes().get(i)));
if (addColumnIdx < shadowSortKeyIdx) {
copiedSortKeyIdxes.set(i, sortKeyIdx + 1);
}
}
}
}
}
if (sortKeyIdxes != null) {
copiedSortKeyIdxes = sortKeyIdxes;
} else if (copiedSortKeyIdxes != null && !copiedSortKeyIdxes.isEmpty()) {
sortKeyIdxes = copiedSortKeyIdxes;
}
for (Tablet shadowTablet : shadowIdx.getTablets()) {
long shadowTabletId = shadowTablet.getId();
LakeTablet lakeTablet = ((LakeTablet) shadowTablet);
Long backendId = Utils.chooseBackend(lakeTablet);
if (backendId == null) {
throw new AlterCancelException("No alive backend");
}
countDownLatch.addMark(backendId, shadowTabletId);
CreateReplicaTask createReplicaTask = new CreateReplicaTask(backendId, dbId, tableId, partitionId,
shadowIdxId, shadowTabletId, shadowShortKeyColumnCount, 0, Partition.PARTITION_INIT_VERSION,
originKeysType, TStorageType.COLUMN, storageMedium, copiedShadowSchema, bfColumns, bfFpp,
countDownLatch, indexes, table.isInMemory(), table.enablePersistentIndex(),
table.primaryIndexCacheExpireSec(), TTabletType.TABLET_TYPE_LAKE, table.getCompressionType(),
copiedSortKeyIdxes);
batchTask.addTask(createReplicaTask);
}
}
}
} catch (Exception e) {
throw new AlterCancelException(e.getMessage());
}
sendAgentTaskAndWait(batchTask, countDownLatch, Config.tablet_create_timeout_second * numTablets);
try (WriteLockedDatabase db = getWriteLockedDatabase(dbId)) {
LakeTable table = getTableOrThrow(db, tableId);
Preconditions.checkState(table.getState() == OlapTable.OlapTableState.SCHEMA_CHANGE);
watershedTxnId = getNextTransactionId();
addShadowIndexToCatalog(table);
}
jobState = JobState.WAITING_TXN;
if (span != null) {
span.setAttribute("watershedTxnId", this.watershedTxnId);
span.addEvent("setWaitingTxn");
}
writeEditLog(this);
LOG.info("transfer schema change job {} state to {}, watershed txn_id: {}", jobId, this.jobState, watershedTxnId);
}


Code after addressing the review:
protected void runPendingJob() throws AlterCancelException {
long numTablets = 0;
AgentBatchTask batchTask = new AgentBatchTask();
MarkedCountDownLatch<Long, Long> countDownLatch;
try (ReadLockedDatabase db = getReadLockedDatabase(dbId)) {
LakeTable table = getTableOrThrow(db, tableId);
Preconditions.checkState(table.getState() == OlapTable.OlapTableState.SCHEMA_CHANGE);
MaterializedIndexMeta indexMeta = table.getIndexMetaByIndexId(table.getBaseIndexId());
numTablets = partitionIndexMap.values().stream().map(MaterializedIndex::getTablets).mapToLong(List::size).sum();
countDownLatch = new MarkedCountDownLatch<>((int) numTablets);
for (long partitionId : partitionIndexMap.rowKeySet()) {
Partition partition = table.getPartition(partitionId);
Preconditions.checkState(partition != null);
TStorageMedium storageMedium = table.getPartitionInfo().getDataProperty(partitionId).getStorageMedium();
Map<Long, MaterializedIndex> shadowIndexMap = partitionIndexMap.row(partitionId);
for (Map.Entry<Long, MaterializedIndex> entry : shadowIndexMap.entrySet()) {
long shadowIdxId = entry.getKey();
MaterializedIndex shadowIdx = entry.getValue();
short shadowShortKeyColumnCount = indexShortKeyMap.get(shadowIdxId);
List<Column> shadowSchema = indexSchemaMap.get(shadowIdxId);
long originIndexId = indexIdMap.get(shadowIdxId);
KeysType originKeysType = table.getKeysTypeByIndexId(originIndexId);
List<Column> originSchema = table.getSchemaByIndexId(originIndexId);
List<Column> copiedShadowSchema = Lists.newArrayList();
for (Column column : shadowSchema) {
Column.DefaultValueType defaultValueType = column.getDefaultValueType();
if (defaultValueType == Column.DefaultValueType.CONST) {
Column copiedColumn = new Column(column);
copiedColumn.setDefaultValue(column.calculatedDefaultValueWithTime(startTime));
copiedShadowSchema.add(copiedColumn);
} else {
copiedShadowSchema.add(column);
}
}
List<Integer> copiedSortKeyIdxes = indexMeta.getSortKeyIdxes();
if (indexMeta.getSortKeyIdxes() != null) {
if (originSchema.size() > shadowSchema.size()) {
List<Column> differences = originSchema.stream().filter(element ->
!shadowSchema.contains(element)).collect(Collectors.toList());
Integer dropIdx = new Integer(originSchema.indexOf(differences.get(0)));
for (int i = 0; i < copiedSortKeyIdxes.size(); ++i) {
Integer sortKeyIdx = copiedSortKeyIdxes.get(i);
if (dropIdx < sortKeyIdx) {
copiedSortKeyIdxes.set(i, sortKeyIdx - 1);
}
}
} else if (originSchema.size() < shadowSchema.size()) {
List<Column> differences = shadowSchema.stream().filter(element ->
!originSchema.contains(element)).collect(Collectors.toList());
for (Column difference : differences) {
int addColumnIdx = shadowSchema.indexOf(difference);
for (int i = 0; i < copiedSortKeyIdxes.size(); ++i) {
Integer sortKeyIdx = copiedSortKeyIdxes.get(i);
int shadowSortKeyIdx = shadowSchema.indexOf(originSchema.get(
indexMeta.getSortKeyIdxes().get(i)));
if (addColumnIdx < shadowSortKeyIdx) {
copiedSortKeyIdxes.set(i, sortKeyIdx + 1);
}
}
}
}
}
if (sortKeyIdxes != null) {
copiedSortKeyIdxes = sortKeyIdxes;
} else if (copiedSortKeyIdxes != null && !copiedSortKeyIdxes.isEmpty()) {
sortKeyIdxes = copiedSortKeyIdxes;
}
for (Tablet shadowTablet : shadowIdx.getTablets()) {
long shadowTabletId = shadowTablet.getId();
LakeTablet lakeTablet = ((LakeTablet) shadowTablet);
Long backendId = Utils.chooseBackend(lakeTablet);
if (backendId == null) {
throw new AlterCancelException("No alive backend");
}
countDownLatch.addMark(backendId, shadowTabletId);
CreateReplicaTask createReplicaTask = new CreateReplicaTask(backendId, dbId, tableId, partitionId,
shadowIdxId, shadowTabletId, shadowShortKeyColumnCount, 0, Partition.PARTITION_INIT_VERSION,
originKeysType, TStorageType.COLUMN, storageMedium, copiedShadowSchema, bfColumns, bfFpp,
countDownLatch, indexes, table.isInMemory(), table.enablePersistentIndex(),
table.primaryIndexCacheExpireSec(), TTabletType.TABLET_TYPE_LAKE, table.getCompressionType(),
copiedSortKeyIdxes);
batchTask.addTask(createReplicaTask);
}
}
}
} catch (Exception e) {
throw new AlterCancelException(e.getMessage());
}
sendAgentTaskAndWait(batchTask, countDownLatch, Config.tablet_create_timeout_second * numTablets);
try (WriteLockedDatabase db = getWriteLockedDatabase(dbId)) {
LakeTable table = getTableOrThrow(db, tableId);
Preconditions.checkState(table.getState() == OlapTable.OlapTableState.SCHEMA_CHANGE);
watershedTxnId = getNextTransactionId();
addShadowIndexToCatalog(table);
}
jobState = JobState.WAITING_TXN;
if (span != null) {
span.setAttribute("watershedTxnId", this.watershedTxnId);
span.addEvent("setWaitingTxn");
}
writeEditLog(this);
LOG.info("transfer schema change job {} state to {}, watershed txn_id: {}", jobId, this.jobState, watershedTxnId);
}