luchunliang commented on a change in pull request #2036:
URL: https://github.com/apache/incubator-inlong/pull/2036#discussion_r775193153



##########
File path: 
inlong-sort-standalone/sort-standalone-source/src/main/java/org/apache/inlong/sort/standalone/source/sortsdk/FetchCallback.java
##########
@@ -0,0 +1,138 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.sort.standalone.source.sortsdk;
+
+import com.google.common.base.Preconditions;
+import org.apache.flume.channel.ChannelProcessor;
+import org.apache.inlong.sdk.sort.api.ReadCallback;
+import org.apache.inlong.sdk.sort.api.SortClient;
+import org.apache.inlong.sdk.sort.entity.MessageRecord;
+import org.apache.inlong.sort.standalone.channel.ProfileEvent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotNull;
+
+/**
+ * Implementation of {@link ReadCallback}.
+ *
+ * TODO: Sort sdk should deliver one object which is held by {@link 
ProfileEvent} and used to ack upstream data store
+ * The code should be like :
+ *
+ *        public void onFinished(final MessageRecord messageRecord, ACKer 
acker) {
+ *            doSomething();
+ *            final ProfileEvent profileEvent = new 
ProfileEvent(result.getBody(), result.getHeaders(), acker);
+ *             channelProcessor.processEvent(profileEvent);
+ *        }
+ *
+ * The ACKer will be used to <b>ACK</b> upstream after that the downstream 
<b>ACKed</b> sort-standalone.
+ * This process seems like <b>transaction</b> of the whole sort-standalone, 
and which
+ * ensure <b>At Least One</b> semantics.
+ */
+public class FetchCallback implements ReadCallback {
+
+    // Logger of {@link FetchCallback}.
+    private static final Logger LOG = 
LoggerFactory.getLogger(FetchCallback.class);
+
+    // SortId of fetch message.
+    private final String sortId;
+
+    // ChannelProcessor that put message in specific channel.
+    private final ChannelProcessor channelProcessor;
+
+    // Context of source, used to report fetch results.
+    private final SortSdkSourceContext context;
+
+    // Temporary usage for ACK. The {@link SortClient} and Callback should not 
circular reference each other.
+    private SortClient client;
+
+    /**
+     * Private constructor of {@link FetchCallback}.
+     * <p> The construction of FetchCallback should be initiated by {@link 
FetchCallback.Factory}.</p>
+     *
+     * @param sortId SortId of fetch message.
+     * @param channelProcessor ChannelProcessor that message put in.
+     * @param context The context to report fetch results.
+     */
+    private FetchCallback(
+            final String sortId,
+            final ChannelProcessor channelProcessor,
+            final SortSdkSourceContext context) {
+        this.sortId = sortId;
+        this.channelProcessor = channelProcessor;
+        this.context = context;
+    }
+
+    /**
+     * Set client for ack.
+     * @param client client for ack.
+     */
+    public void setClient(@NotNull SortClient client) {
+        this.client = client;
+    }
+
+    /**
+     * The callback function that SortSDK invoke when fetch messages.
+     *
+     * <p> In order to ACK the fetched msg, {@link FetchCallback} has to hold 
the {@link SortClient} which results in
+     * <b>Cycle Reference</b>. The {@link SortClient} should deliver one 
object to ACK after invoke the callback method
+     * {@link ReadCallback#onFinished(MessageRecord)}. </p>
+     *
+     * @param messageRecord message
+     */
+    @Override
+    public void onFinished(final MessageRecord messageRecord) {
+        try {
+            Preconditions.checkState(messageRecord != null, "Fetched msg is 
null.");
+            final SubscribeFetchResult result = 
SubscribeFetchResult.Factory.create(sortId, messageRecord);
+            final ProfileEvent profileEvent = new 
ProfileEvent(result.getBody(), result.getHeaders());
+            channelProcessor.processEvent(profileEvent);
+            context.reportToMetric(profileEvent, sortId, "-", 
SortSdkSourceContext.FetchResult.SUCCESS);
+            client.ack(messageRecord.getMsgKey(), messageRecord.getMsgKey());

Review comment:
       The acking operation need to move to ProfileEvent. 
   In the future, execute acking operation after sending successfully.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to