egor-ryashin commented on a change in pull request #7038: Fix and document 
concurrency of EventReceiverFirehose and TimedShutoffFirehose; Refine 
concurrency specification of Firehose
URL: https://github.com/apache/incubator-druid/pull/7038#discussion_r255305171
 
 

 ##########
 File path: 
server/src/main/java/org/apache/druid/segment/realtime/firehose/EventReceiverFirehoseFactory.java
 ##########
 @@ -453,61 +569,57 @@ public boolean isClosed()
       final String producerId = req.getHeader("X-Firehose-Producer-Id");
 
       if (producerId == null) {
-        return Optional.empty();
+        return null;
       }
 
       final String sequenceValue = req.getHeader("X-Firehose-Producer-Seq");
 
       if (sequenceValue == null) {
-        return Optional.of(
-            Response.status(Response.Status.BAD_REQUEST)
-                       .entity(ImmutableMap.<String, Object>of("error", 
"Producer sequence value is missing"))
-                       .build()
-        );
+        return Response
+            .status(Response.Status.BAD_REQUEST)
+            .entity(ImmutableMap.<String, Object>of("error", "Producer 
sequence value is missing"))
+            .build();
       }
 
       Long producerSequence = producerSequences.computeIfAbsent(producerId, 
key -> Long.MIN_VALUE);
 
       if (producerSequences.size() >= MAX_FIREHOSE_PRODUCERS) {
-        return Optional.of(
-            Response.status(Response.Status.FORBIDDEN)
-                    .entity(
-                        ImmutableMap.<String, Object>of(
-                            "error",
-                            "Too many individual producer IDs for this 
firehose.  Max is " + MAX_FIREHOSE_PRODUCERS
-                        )
-                    )
-                    .build()
-        );
+        return Response
+            .status(Response.Status.FORBIDDEN)
+            .entity(
+                ImmutableMap.of(
+                    "error",
+                    "Too many individual producer IDs for this firehose.  Max 
is " + MAX_FIREHOSE_PRODUCERS
+                )
+            )
+            .build();
       }
 
       try {
         Long newSequence = Long.parseLong(sequenceValue);
-        if (newSequence <= producerSequence) {
-          return Optional.of(
-              Response.ok(
-                  responseMapper.writeValueAsString(
-                      ImmutableMap.of("eventCount", 0, "skipped", true)
-                  ),
-                  responseContentType
-              ).build()
-          );
-        }
 
-        producerSequences.put(producerId, newSequence);
+        while (true) {
+          if (newSequence <= producerSequence) {
+            return Response.ok(
+                
responseMapper.writeValueAsString(ImmutableMap.of("eventCount", 0, "skipped", 
true)),
+                responseContentType
+            ).build();
+          }
+          if (producerSequences.replace(producerId, producerSequence, 
newSequence)) {
+            return null;
+          }
+          producerSequence = producerSequences.get(producerId);
 
 Review comment:
   I believe this method shouldn't be called by multiple threads of the same 
producer, as we don't guarantee exactly-once event processing that way, this 
code block actually detects an illegal state of the application environment (a 
single producerId with multiple threads) and we should report an error 
different from `skipped`.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to