-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
430 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 12 additions & 1 deletion
13
...-queue/src/main/java/org/swisspush/gateleen/queue/queuing/splitter/NoOpQueueSplitter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,26 @@ | ||
package org.swisspush.gateleen.queue.queuing.splitter; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.core.Promise; | ||
import io.vertx.core.http.HttpServerRequest; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public class NoOpQueueSplitter implements QueueSplitter { | ||
|
||
@Override | ||
public Future<Void> initialize() { | ||
Promise<Void> promise = Promise.promise(); | ||
promise.complete(); | ||
return promise.future(); | ||
} | ||
|
||
@Override | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public String convertToSubQueue(String queue) { | ||
public String convertToSubQueue(String queue, HttpServerRequest request) { | ||
return queue; | ||
} | ||
} |
23 changes: 15 additions & 8 deletions
23
...leen-queue/src/main/java/org/swisspush/gateleen/queue/queuing/splitter/QueueSplitter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,24 @@ | ||
package org.swisspush.gateleen.queue.queuing.splitter; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.core.http.HttpServerRequest; | ||
|
||
/** | ||
* Interface for queues configured to be split in sub-queues. The method {@link QueueSplitter#convertToSubQueue(String)} | ||
* Interface for queues configured to be split in sub-queues. The method {@link QueueSplitter#convertToSubQueue(String, HttpServerRequest)} | ||
* evaluates the convert of the queue name in a sub-queue name. | ||
* | ||
* @author https://github.com/gcastaldi [Giannandrea Castaldi] | ||
*/ | ||
public interface QueueSplitter { | ||
/** | ||
* Convert the queue name in a sub-queue name. If not necessary maintains the initial queue name. | ||
* | ||
* @param queue | ||
* @return sub-queue name | ||
*/ | ||
String convertToSubQueue(String queue); | ||
|
||
public Future<Void> initialize(); | ||
|
||
/** | ||
* Convert the queue name in a sub-queue name. If not necessary maintains the initial queue name. | ||
* | ||
* @param queue | ||
* @param request | ||
* @return sub-queue name | ||
*/ | ||
String convertToSubQueue(String queue, HttpServerRequest request); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 59 additions & 8 deletions
67
...-queue/src/main/java/org/swisspush/gateleen/queue/queuing/splitter/QueueSplitterImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,95 @@ | ||
package org.swisspush.gateleen.queue.queuing.splitter; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.core.Promise; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.core.http.HttpServerRequest; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.swisspush.gateleen.core.configuration.ConfigurationResourceConsumer; | ||
import org.swisspush.gateleen.core.configuration.ConfigurationResourceManager; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public class QueueSplitterImpl extends ConfigurationResourceConsumer implements QueueSplitter { | ||
|
||
private final Logger log = LoggerFactory.getLogger(QueueSplitterImpl.class); | ||
|
||
private final Map<String, Object> properties; | ||
|
||
private boolean initialized = false; | ||
|
||
public QueueSplitterImpl( | ||
ConfigurationResourceManager configurationResourceManager, | ||
String configResourceUri | ||
) { | ||
this(configurationResourceManager, configResourceUri, "gateleen_queue_splitter_configuration_schema"); | ||
this(configurationResourceManager, configResourceUri, new HashMap<>()); | ||
} | ||
|
||
public QueueSplitterImpl( | ||
ConfigurationResourceManager configurationResourceManager, | ||
String configResourceUri, | ||
String schemaResourceName | ||
Map<String, Object> properties | ||
) { | ||
super(configurationResourceManager, configResourceUri, schemaResourceName); | ||
super(configurationResourceManager, configResourceUri, "gateleen_queue_splitter_configuration_schema"); | ||
this.properties = properties; | ||
} | ||
|
||
@Override | ||
public void resourceChanged(String resourceUri, Buffer resource) { | ||
public Future<Void> initialize() { | ||
Promise<Void> promise = Promise.promise(); | ||
configurationResourceManager().getRegisteredResource(configResourceUri()).onComplete((event -> { | ||
if (event.succeeded() && event.result().isPresent()) { | ||
initializeQueueSplitterConfiguration(event.result().get()).onComplete((event1 -> promise.complete())); | ||
} else { | ||
log.warn("No queue splitter configuration resource with uri '{}' found. Unable to setup kafka configuration correctly", configResourceUri()); | ||
promise.complete(); | ||
} | ||
})); | ||
return promise.future(); | ||
} | ||
|
||
public boolean isInitialized() { | ||
return initialized; | ||
} | ||
|
||
@Override | ||
public void resourceRemoved(String resourceUri) { | ||
private Future<Void> initializeQueueSplitterConfiguration(Buffer configuration) { | ||
Promise<Void> promise = Promise.promise(); | ||
final List<QueueSplitterConfiguration> kafkaConfigurations = QueueSplitterConfigurationParser.parse(configuration, properties); | ||
|
||
// TODO: release all splitters and creates new ones | ||
|
||
return promise.future(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public String convertToSubQueue(final String queue) { | ||
public String convertToSubQueue(final String queue, HttpServerRequest request) { | ||
return queue + "-1"; | ||
} | ||
|
||
@Override | ||
public void resourceChanged(String resourceUri, Buffer resource) { | ||
if (configResourceUri() != null && configResourceUri().equals(resourceUri)) { | ||
log.info("Queue splitter configuration resource {} was updated. Going to initialize with new configuration", resourceUri); | ||
initializeQueueSplitterConfiguration(resource); | ||
} | ||
} | ||
|
||
@Override | ||
public void resourceRemoved(String resourceUri) { | ||
if (configResourceUri() != null && configResourceUri().equals(resourceUri)) { | ||
log.info("Queue splitter configuration resource {} was removed. Going to close all kafka producers", resourceUri); | ||
|
||
// TODO: release all splitters and creates new ones | ||
|
||
initialized = false; | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...main/java/org/swisspush/gateleen/queue/queuing/splitter/executors/QueueSplitExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.swisspush.gateleen.queue.queuing.splitter.executors; | ||
|
||
import io.vertx.core.http.HttpServerRequest; | ||
|
||
public interface QueueSplitExecutor { | ||
|
||
boolean matches(String queue); | ||
|
||
String executeSplit(String queue, HttpServerRequest request); | ||
} |
17 changes: 17 additions & 0 deletions
17
.../java/org/swisspush/gateleen/queue/queuing/splitter/executors/QueueSplitExecutorBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.swisspush.gateleen.queue.queuing.splitter.executors; | ||
|
||
import org.swisspush.gateleen.queue.queuing.splitter.QueueSplitterConfiguration; | ||
|
||
public abstract class QueueSplitExecutorBase implements QueueSplitExecutor { | ||
|
||
protected final QueueSplitterConfiguration configuration; | ||
|
||
protected QueueSplitExecutorBase(QueueSplitterConfiguration configuration) { | ||
this.configuration = configuration; | ||
} | ||
|
||
@Override | ||
public boolean matches(String queue) { | ||
return configuration.getQueue().matcher(queue).matches(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...a/org/swisspush/gateleen/queue/queuing/splitter/executors/QueueSplitExecutorFromList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.swisspush.gateleen.queue.queuing.splitter.executors; | ||
|
||
import io.vertx.core.http.HttpServerRequest; | ||
import org.swisspush.gateleen.queue.queuing.splitter.QueueSplitterConfiguration; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class QueueSplitExecutorFromList extends QueueSplitExecutorBase { | ||
|
||
AtomicInteger atomicInteger = new AtomicInteger(0); | ||
|
||
protected QueueSplitExecutorFromList(QueueSplitterConfiguration configuration) { | ||
super(configuration); | ||
} | ||
|
||
@Override | ||
public String executeSplit(String queue, HttpServerRequest request) { | ||
StringBuilder stringBuilder = new StringBuilder(queue); | ||
if (matches(queue)) { | ||
stringBuilder.append(configuration.getPostfixDelimiter()); | ||
stringBuilder.append(configuration.getPostfixFromStatic().get( | ||
atomicInteger.getAndAccumulate( | ||
1, | ||
(left, right) -> (left + right) % configuration.getPostfixFromStatic().size() | ||
) | ||
)); | ||
} | ||
return stringBuilder.toString(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...rg/swisspush/gateleen/queue/queuing/splitter/executors/QueueSplitExecutorFromRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.swisspush.gateleen.queue.queuing.splitter.executors; | ||
|
||
import io.vertx.core.http.HttpServerRequest; | ||
import org.swisspush.gateleen.queue.queuing.splitter.QueueSplitterConfiguration; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class QueueSplitExecutorFromRequest extends QueueSplitExecutorBase { | ||
protected QueueSplitExecutorFromRequest(QueueSplitterConfiguration configuration) { | ||
super(configuration); | ||
} | ||
|
||
@Override | ||
public String executeSplit(String queue, HttpServerRequest request) { | ||
StringBuilder stringBuilder = new StringBuilder(queue); | ||
if (matches(queue)) { | ||
if (configuration.getPostfixFromUrl() != null) { | ||
Pattern pattern = Pattern.compile(configuration.getPostfixFromUrl()); | ||
Matcher matcher = pattern.matcher(request.uri()); | ||
if (matcher.matches()) { | ||
for (int i = 0; i < matcher.groupCount(); i++) { | ||
stringBuilder.append(configuration.getPostfixDelimiter()); | ||
stringBuilder.append(matcher.group(i + 1)); | ||
} | ||
} | ||
} | ||
if (configuration.getPostfixFromHeader() != null) { | ||
stringBuilder.append(configuration.getPostfixDelimiter()); | ||
stringBuilder.append(request.headers().get(configuration.getPostfixFromHeader())); | ||
} | ||
} | ||
return stringBuilder.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.