|
18 | 18 |
|
19 | 19 | package org.apache.amoro.server.dashboard; |
20 | 20 |
|
| 21 | +import static org.apache.amoro.table.TablePartitionDetailProperties.BASE_FILE_COUNT; |
| 22 | +import static org.apache.amoro.table.TablePartitionDetailProperties.BASE_FILE_COUNT_DEFAULT; |
| 23 | +import static org.apache.amoro.table.TablePartitionDetailProperties.EQ_DELETE_FILE_COUNT; |
| 24 | +import static org.apache.amoro.table.TablePartitionDetailProperties.EQ_DELETE_FILE_COUNT_DEFAULT; |
| 25 | +import static org.apache.amoro.table.TablePartitionDetailProperties.FILE_SIZE_SQUARED_ERROR_SUM; |
| 26 | +import static org.apache.amoro.table.TablePartitionDetailProperties.FILE_SIZE_SQUARED_ERROR_SUM_DEFAULT; |
| 27 | +import static org.apache.amoro.table.TablePartitionDetailProperties.INSERT_FILE_COUNT; |
| 28 | +import static org.apache.amoro.table.TablePartitionDetailProperties.INSERT_FILE_COUNT_DEFAULT; |
| 29 | +import static org.apache.amoro.table.TablePartitionDetailProperties.POS_DELETE_FILE_COUNT; |
| 30 | +import static org.apache.amoro.table.TablePartitionDetailProperties.POS_DELETE_FILE_COUNT_DEFAULT; |
| 31 | + |
21 | 32 | import com.github.pagehelper.Page; |
22 | 33 | import com.github.pagehelper.PageHelper; |
23 | 34 | import com.github.pagehelper.PageInfo; |
@@ -451,19 +462,132 @@ public List<PartitionBaseInfo> getTablePartitions(AmoroTable<?> amoroTable) { |
451 | 462 | getTableFilesInternal(amoroTable, null, null); |
452 | 463 | try { |
453 | 464 | for (PartitionFileBaseInfo fileInfo : tableFiles) { |
454 | | - if (!partitionBaseInfoHashMap.containsKey(fileInfo.getPartition())) { |
455 | | - PartitionBaseInfo partitionBaseInfo = new PartitionBaseInfo(); |
456 | | - partitionBaseInfo.setPartition(fileInfo.getPartition()); |
457 | | - partitionBaseInfo.setSpecId(fileInfo.getSpecId()); |
458 | | - partitionBaseInfoHashMap.put(fileInfo.getPartition(), partitionBaseInfo); |
459 | | - } |
460 | | - PartitionBaseInfo partitionInfo = partitionBaseInfoHashMap.get(fileInfo.getPartition()); |
461 | | - partitionInfo.setFileCount(partitionInfo.getFileCount() + 1); |
462 | | - partitionInfo.setFileSize(partitionInfo.getFileSize() + fileInfo.getFileSize()); |
463 | | - partitionInfo.setLastCommitTime( |
464 | | - partitionInfo.getLastCommitTime() > fileInfo.getCommitTime() |
465 | | - ? partitionInfo.getLastCommitTime() |
466 | | - : fileInfo.getCommitTime()); |
| 465 | + refreshPartitionBasicInfo(fileInfo, partitionBaseInfoHashMap); |
| 466 | + } |
| 467 | + } finally { |
| 468 | + try { |
| 469 | + tableFiles.close(); |
| 470 | + } catch (IOException e) { |
| 471 | + LOG.warn("Failed to close the manifest reader.", e); |
| 472 | + } |
| 473 | + } |
| 474 | + return new ArrayList<>(partitionBaseInfoHashMap.values()); |
| 475 | + } |
| 476 | + |
| 477 | + /** |
| 478 | + * Create partition base information from a PartitionFileBaseInfo instance. |
| 479 | + * |
| 480 | + * @param fileInfo Partition file base information, used to obtain partition information |
| 481 | + * @return Returns the partition base information corresponding to the partition |
| 482 | + */ |
| 483 | + private PartitionBaseInfo createPartitionBaseInfoFromPartitionFile( |
| 484 | + PartitionFileBaseInfo fileInfo) { |
| 485 | + PartitionBaseInfo partitionBaseInfo = new PartitionBaseInfo(); |
| 486 | + partitionBaseInfo.setPartition(fileInfo.getPartition()); |
| 487 | + partitionBaseInfo.setSpecId(fileInfo.getSpecId()); |
| 488 | + return partitionBaseInfo; |
| 489 | + } |
| 490 | + |
| 491 | + /** |
| 492 | + * Refresh the basic information of a partition |
| 493 | + * |
| 494 | + * @param fileInfo Partition file base information |
| 495 | + * @param partitionBaseInfoHashMap A hashmap containing the base information of all partitions |
| 496 | + */ |
| 497 | + private void refreshPartitionBasicInfo( |
| 498 | + PartitionFileBaseInfo fileInfo, Map<String, PartitionBaseInfo> partitionBaseInfoHashMap) { |
| 499 | + // Get the partitionBaseInfo instance |
| 500 | + PartitionBaseInfo partitionInfo = |
| 501 | + partitionBaseInfoHashMap.computeIfAbsent( |
| 502 | + fileInfo.getPartition(), key -> createPartitionBaseInfoFromPartitionFile(fileInfo)); |
| 503 | + // Update the number of files |
| 504 | + partitionInfo.setFileCount(partitionInfo.getFileCount() + 1); |
| 505 | + // Update the total file size |
| 506 | + partitionInfo.setFileSize(partitionInfo.getFileSize() + fileInfo.getFileSize()); |
| 507 | + // Update the last commit time |
| 508 | + partitionInfo.setLastCommitTime( |
| 509 | + partitionInfo.getLastCommitTime() > fileInfo.getCommitTime() |
| 510 | + ? partitionInfo.getLastCommitTime() |
| 511 | + : fileInfo.getCommitTime()); |
| 512 | + } |
| 513 | + |
| 514 | + /** |
| 515 | + * Refresh and update the detailed properties of a partition based on file information. |
| 516 | + * |
| 517 | + * <p>This method primarily updates statistical properties of the partition, such as the sum of |
| 518 | + * squared errors of file sizes, and the counts of base files, insert files, eq-delete files, and |
| 519 | + * pos-delete files.</> |
| 520 | + * |
| 521 | + * @param fileInfo Partition file base information |
| 522 | + * @param partitionBaseInfoHashMap A hashmap containing basic information about all partitions |
| 523 | + * @param minTargetSize The minimum target size used to limit the file size and calculate the sum |
| 524 | + * of squared errors. |
| 525 | + */ |
| 526 | + private void refreshPartitionDetailProperties( |
| 527 | + PartitionFileBaseInfo fileInfo, |
| 528 | + Map<String, PartitionBaseInfo> partitionBaseInfoHashMap, |
| 529 | + long minTargetSize) { |
| 530 | + PartitionBaseInfo partitionInfo = |
| 531 | + partitionBaseInfoHashMap.computeIfAbsent( |
| 532 | + fileInfo.getPartition(), key -> createPartitionBaseInfoFromPartitionFile(fileInfo)); |
| 533 | + // Update the file-size-squared-error-sum |
| 534 | + long actualSize = Math.min(fileInfo.getFileSize(), minTargetSize); |
| 535 | + long diff = minTargetSize - actualSize; |
| 536 | + partitionInfo.setProperty( |
| 537 | + FILE_SIZE_SQUARED_ERROR_SUM, |
| 538 | + (double) |
| 539 | + partitionInfo.getPropertyOrDefault( |
| 540 | + FILE_SIZE_SQUARED_ERROR_SUM, FILE_SIZE_SQUARED_ERROR_SUM_DEFAULT) |
| 541 | + + diff * diff); |
| 542 | + |
| 543 | + // Update the count of base files, insert files, equality delete files, and position delete |
| 544 | + // files |
| 545 | + switch (DataFileType.fromName(fileInfo.getFileType())) { |
| 546 | + case BASE_FILE: |
| 547 | + partitionInfo.setProperty( |
| 548 | + BASE_FILE_COUNT, |
| 549 | + (long) partitionInfo.getPropertyOrDefault(BASE_FILE_COUNT, BASE_FILE_COUNT_DEFAULT) |
| 550 | + + 1); |
| 551 | + break; |
| 552 | + case INSERT_FILE: |
| 553 | + partitionInfo.setProperty( |
| 554 | + INSERT_FILE_COUNT, |
| 555 | + (long) partitionInfo.getPropertyOrDefault(INSERT_FILE_COUNT, INSERT_FILE_COUNT_DEFAULT) |
| 556 | + + 1); |
| 557 | + break; |
| 558 | + case EQ_DELETE_FILE: |
| 559 | + partitionInfo.setProperty( |
| 560 | + EQ_DELETE_FILE_COUNT, |
| 561 | + (long) |
| 562 | + partitionInfo.getPropertyOrDefault( |
| 563 | + EQ_DELETE_FILE_COUNT, EQ_DELETE_FILE_COUNT_DEFAULT) |
| 564 | + + 1); |
| 565 | + break; |
| 566 | + case POS_DELETE_FILE: |
| 567 | + partitionInfo.setProperty( |
| 568 | + POS_DELETE_FILE_COUNT, |
| 569 | + (long) |
| 570 | + partitionInfo.getPropertyOrDefault( |
| 571 | + POS_DELETE_FILE_COUNT, POS_DELETE_FILE_COUNT_DEFAULT) |
| 572 | + + 1); |
| 573 | + break; |
| 574 | + } |
| 575 | + } |
| 576 | + |
| 577 | + /** |
| 578 | + * Get a list of partition information for a specific table that contains the square error sum of |
| 579 | + * the partition file size. This method calculates statistics for each partition of a table, |
| 580 | + * including file count, total file size, and the sum of squared errors of file sizes, for use in |
| 581 | + * further operations such as table optimization. |
| 582 | + */ |
| 583 | + public List<PartitionBaseInfo> getTablePartitionsWithDetailProperties( |
| 584 | + MixedTable table, long minTargetSize) { |
| 585 | + Map<String, PartitionBaseInfo> partitionBaseInfoHashMap = new HashMap<>(); |
| 586 | + CloseableIterable<PartitionFileBaseInfo> tableFiles = getTableFilesInternal(table, null, null); |
| 587 | + try { |
| 588 | + for (PartitionFileBaseInfo fileInfo : tableFiles) { |
| 589 | + refreshPartitionBasicInfo(fileInfo, partitionBaseInfoHashMap); |
| 590 | + refreshPartitionDetailProperties(fileInfo, partitionBaseInfoHashMap, minTargetSize); |
467 | 591 | } |
468 | 592 | } finally { |
469 | 593 | try { |
@@ -609,6 +733,11 @@ public List<OptimizingTaskInfo> getOptimizingTaskInfos( |
609 | 733 | private CloseableIterable<PartitionFileBaseInfo> getTableFilesInternal( |
610 | 734 | AmoroTable<?> amoroTable, String partition, Integer specId) { |
611 | 735 | MixedTable mixedTable = getTable(amoroTable); |
| 736 | + return getTableFilesInternal(mixedTable, partition, specId); |
| 737 | + } |
| 738 | + |
| 739 | + private CloseableIterable<PartitionFileBaseInfo> getTableFilesInternal( |
| 740 | + MixedTable mixedTable, String partition, Integer specId) { |
612 | 741 | if (mixedTable.isKeyedTable()) { |
613 | 742 | return CloseableIterable.concat( |
614 | 743 | Arrays.asList( |
|
0 commit comments