1818
1919package org .apache .paimon .flink .sink .coordinator ;
2020
21- import org .apache .paimon .CoreOptions ;
22- import org .apache .paimon .Snapshot ;
23- import org .apache .paimon .data .BinaryRow ;
2421import org .apache .paimon .flink .sink .TableWriteOperator ;
25- import org .apache .paimon .fs .Path ;
26- import org .apache .paimon .index .IndexFileHandler ;
27- import org .apache .paimon .index .IndexFileMeta ;
28- import org .apache .paimon .io .DataFileMeta ;
29- import org .apache .paimon .manifest .ManifestEntry ;
30- import org .apache .paimon .operation .FileStoreScan ;
31- import org .apache .paimon .operation .write .WriteRestore ;
32- import org .apache .paimon .options .MemorySize ;
3322import org .apache .paimon .table .FileStoreTable ;
34- import org .apache .paimon .utils .SegmentsCache ;
3523
3624import org .apache .flink .runtime .jobgraph .OperatorID ;
3725import org .apache .flink .runtime .operators .coordination .CoordinationRequest ;
4028import org .apache .flink .runtime .operators .coordination .OperatorCoordinator ;
4129import org .apache .flink .runtime .operators .coordination .OperatorEvent ;
4230
43- import java .io .IOException ;
44- import java .util .ArrayList ;
45- import java .util .List ;
46- import java .util .Map ;
47- import java .util .Optional ;
4831import java .util .concurrent .CompletableFuture ;
49- import java .util .concurrent .ConcurrentHashMap ;
5032import java .util .concurrent .ThreadPoolExecutor ;
5133
52- import static org .apache .paimon .deletionvectors .DeletionVectorsIndexFile .DELETION_VECTORS_INDEX ;
53- import static org .apache .paimon .flink .FlinkConnectorOptions .SINK_WRITER_COORDINATOR_CACHE_MEMORY ;
54- import static org .apache .paimon .utils .SerializationUtils .deserializeBinaryRow ;
5534import static org .apache .paimon .utils .ThreadPoolUtils .createCachedThreadPool ;
5635
5736/**
@@ -63,88 +42,16 @@ public class WriteOperatorCoordinator implements OperatorCoordinator, Coordinati
6342 private final FileStoreTable table ;
6443
6544 private ThreadPoolExecutor executor ;
66- private Map <String , Long > latestCommittedIdentifiers ;
67-
68- private volatile Snapshot snapshot ;
69- private volatile FileStoreScan dataFileScan ;
70- private volatile IndexFileHandler indexFileHandler ;
45+ private TableWriteCoordinator coordinator ;
7146
7247 public WriteOperatorCoordinator (FileStoreTable table ) {
7348 this .table = table ;
7449 }
7550
76- private synchronized void refreshOrCreateScan () {
77- Optional <Snapshot > latestSnapshot = table .latestSnapshot ();
78- if (!latestSnapshot .isPresent ()) {
79- return ;
80- }
81- if (dataFileScan == null ) {
82- dataFileScan = table .store ().newScan ();
83- if (table .coreOptions ().manifestDeleteFileDropStats ()) {
84- dataFileScan .dropStats ();
85- }
86- }
87- if (indexFileHandler == null ) {
88- indexFileHandler = table .store ().newIndexFileHandler ();
89- }
90- snapshot = latestSnapshot .get ();
91- dataFileScan .withSnapshot (snapshot );
92- }
93-
94- private synchronized ScanCoordinationResponse scanDataFiles (ScanCoordinationRequest request )
95- throws IOException {
96- if (snapshot == null ) {
97- return new ScanCoordinationResponse (null , null , null , null , null );
98- }
99-
100- BinaryRow partition = deserializeBinaryRow (request .partition ());
101- int bucket = request .bucket ();
102-
103- List <DataFileMeta > restoreFiles = new ArrayList <>();
104- List <ManifestEntry > entries =
105- dataFileScan .withPartitionBucket (partition , bucket ).plan ().files ();
106- Integer totalBuckets = WriteRestore .extractDataFiles (entries , restoreFiles );
107-
108- IndexFileMeta dynamicBucketIndex = null ;
109- if (request .scanDynamicBucketIndex ()) {
110- dynamicBucketIndex =
111- indexFileHandler .scanHashIndex (snapshot , partition , bucket ).orElse (null );
112- }
113-
114- List <IndexFileMeta > deleteVectorsIndex = null ;
115- if (request .scanDeleteVectorsIndex ()) {
116- deleteVectorsIndex =
117- indexFileHandler .scan (snapshot , DELETION_VECTORS_INDEX , partition , bucket );
118- }
119-
120- return new ScanCoordinationResponse (
121- snapshot , totalBuckets , restoreFiles , dynamicBucketIndex , deleteVectorsIndex );
122- }
123-
124- private synchronized LatestIdentifierResponse latestCommittedIdentifier (
125- LatestIdentifierRequest request ) {
126- String user = request .user ();
127- long identifier =
128- latestCommittedIdentifiers .computeIfAbsent (
129- user ,
130- k ->
131- table .snapshotManager ()
132- .latestSnapshotOfUser (user )
133- .map (Snapshot ::commitIdentifier )
134- .orElse (Long .MIN_VALUE ));
135- return new LatestIdentifierResponse (identifier );
136- }
137-
13851 @ Override
13952 public void start () throws Exception {
14053 executor = createCachedThreadPool (1 , "WriteCoordinator" );
141- latestCommittedIdentifiers = new ConcurrentHashMap <>();
142- CoreOptions options = table .coreOptions ();
143- MemorySize cacheMemory =
144- options .toConfiguration ().get (SINK_WRITER_COORDINATOR_CACHE_MEMORY );
145- SegmentsCache <Path > manifestCache = SegmentsCache .create (cacheMemory , Long .MAX_VALUE );
146- table .setManifestCache (manifestCache );
147- refreshOrCreateScan ();
54+ coordinator = new TableWriteCoordinator (table );
14855 }
14956
15057 @ Override
@@ -163,10 +70,12 @@ public CompletableFuture<CoordinationResponse> handleCoordinationRequest(
16370 () -> {
16471 try {
16572 if (request instanceof ScanCoordinationRequest ) {
166- future .complete (scanDataFiles ((ScanCoordinationRequest ) request ));
73+ future .complete (coordinator . scan ((ScanCoordinationRequest ) request ));
16774 } else if (request instanceof LatestIdentifierRequest ) {
16875 future .complete (
169- latestCommittedIdentifier ((LatestIdentifierRequest ) request ));
76+ new LatestIdentifierResponse (
77+ coordinator .latestCommittedIdentifier (
78+ ((LatestIdentifierRequest ) request ).user ())));
17079 } else {
17180 throw new UnsupportedOperationException (
17281 "Unsupported request type: " + request );
@@ -180,10 +89,7 @@ public CompletableFuture<CoordinationResponse> handleCoordinationRequest(
18089
18190 @ Override
18291 public void checkpointCoordinator (long checkpointId , CompletableFuture <byte []> resultFuture ) {
183- // refresh latest snapshot for data & index files scan
184- refreshOrCreateScan ();
185- // refresh latest committed identifiers for all users
186- latestCommittedIdentifiers .clear ();
92+ coordinator .checkpoint ();
18793 }
18894
18995 @ Override
0 commit comments