chibenwa commented on a change in pull request #808:
URL: https://github.com/apache/james-project/pull/808#discussion_r780181972



##########
File path: 
server/queue/queue-pulsar/src/main/scala/org/apache/james/queue/pulsar/PulsarMailQueue.scala
##########
@@ -0,0 +1,600 @@
+/****************************************************************
+ * 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.james.queue.pulsar
+
+import akka.actor.{ActorRef, ActorSystem}
+import akka.stream.scaladsl.{Flow, Keep, RunnableGraph, Sink, Source, 
SourceQueueWithComplete, StreamConverters}
+import akka.stream.{Attributes, OverflowStrategy}
+import akka.util.Timeout
+import akka.{Done, NotUsed}
+import com.sksamuel.pulsar4s._
+import com.sksamuel.pulsar4s.akka.streams
+import com.sksamuel.pulsar4s.akka.streams.{CommittableMessage, Control}
+import org.apache.james.backends.pulsar.{PulsarConfiguration, PulsarReader}
+import org.apache.james.blob.api.{BlobId, Store}
+import org.apache.james.blob.mail.MimeMessagePartsId
+import org.apache.james.core.{MailAddress, MaybeSender}
+import org.apache.james.metrics.api.{GaugeRegistry, MetricFactory}
+import org.apache.james.queue.api.MailQueue._
+import org.apache.james.queue.api._
+import org.apache.james.queue.pulsar.EnqueueId.EnqueueId
+import org.apache.james.server.core.MailImpl
+import org.apache.mailet._
+import org.apache.pulsar.client.admin.PulsarAdmin
+import org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException
+import org.apache.pulsar.client.api.{Schema, SubscriptionInitialPosition, 
SubscriptionType}
+import org.reactivestreams.Publisher
+import play.api.libs.json._
+
+import java.time.{Instant, ZonedDateTime, Duration => JavaDuration}
+import java.util.concurrent.TimeUnit
+import java.util.{Date, UUID}
+import javax.mail.MessagingException
+import javax.mail.internet.MimeMessage
+import scala.concurrent.duration._
+import scala.concurrent.{Await, ExecutionContext, ExecutionContextExecutor, 
Future, Promise}
+import scala.jdk.CollectionConverters._
+import scala.jdk.DurationConverters._
+import scala.math.Ordered.orderingToOrdered
+
+private[pulsar] object serializers {
+  implicit val headerFormat: Format[Header] = Json.format[Header]
+  implicit val enqueueIdFormat: Format[EnqueueId] = new Format[EnqueueId] {
+    override def writes(o: EnqueueId): JsValue = JsString(o.value)
+
+    override def reads(json: JsValue): JsResult[EnqueueId] =
+      json.validate[String].map(EnqueueId.apply).flatMap(_.fold(JsError.apply, 
JsSuccess(_)))
+  }
+  implicit val mailMetadataFormat: Format[MailMetadata] = 
Json.format[MailMetadata]
+}
+
+private[pulsar] object schemas {
+  implicit val schema: Schema[String] = Schema.STRING
+}
+
+/**
+ * In order to implement removal of mails from the queue, `PulsarMailQueue` 
makes use of a topic
+ * in which some filters are pushed. That way, all instances of 
`PulsarMailQueue` in a cluster
+ * eventually start dropping mails matching filters, effectively removing them 
from mail processing.
+ *
+ * The filtering is handled by a `FilterStage` Actor which maintains a set of 
active filters published by
+ * the `remove` method. It is responsible for dropping filters that can no 
longer match any message, and providing
+ * a consistent view of the messages that will be processed to the `browse` 
method.
+ *
+ * A filter cannot remove messages that are enqueued after the call to the 
`remove` method.
+ */
+class PulsarMailQueue(
+  name: MailQueueName,
+  config: PulsarConfiguration,
+  blobIdFactory: BlobId.Factory,
+  mimeMessageStore: Store[MimeMessage, MimeMessagePartsId],
+  mailQueueItemDecoratorFactory: MailQueueItemDecoratorFactory,
+  metricFactory: MetricFactory,
+  gaugeRegistry: GaugeRegistry,
+  system: ActorSystem
+) extends MailQueue with ManageableMailQueue {
+
+  import schemas._
+  import serializers._
+
+  type MessageAsJson = String
+
+  private val enqueueBufferSize = 10
+  private val requeueBufferSize = 10
+  private val awaitTimeout = 10.seconds
+
+  gaugeRegistry.register(QUEUE_SIZE_METRIC_NAME_PREFIX + name, () => getSize)
+  private val dequeueMetrics = 
metricFactory.generate(DEQUEUED_METRIC_NAME_PREFIX + name.asString)
+  private val enqueueMetric = 
metricFactory.generate(ENQUEUED_METRIC_NAME_PREFIX + name.asString)
+
+  private implicit val implicitSystem: ActorSystem = system
+  private implicit val ec: ExecutionContextExecutor = system.dispatcher
+  private implicit val implicitBlobIdFactory: BlobId.Factory = blobIdFactory
+  private implicit val client: PulsarAsyncClient = 
PulsarClient(config.brokerUri)
+  private val admin = {
+    val builder = PulsarAdmin.builder()
+    builder.serviceHttpUrl(config.adminUri).build()
+  }
+
+  private val outTopic = 
Topic(s"persistent://${config.namespace.asString}/James-${name.asString()}")
+  private val scheduledTopic = 
Topic(s"persistent://${config.namespace.asString}/${name.asString()}-scheduled")

Review comment:
       Likely a stupid question but why using a separate topic for delayed 
messages?
   
   My take is that it limits the out-of-order impact and yields better 
performance.
   
   Do you confirm?




-- 
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]



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

Reply via email to