Skip to content

Commit

Permalink
HCHttp refactoring - splitting HCHttp and ClientObjectFactory (#60)
Browse files Browse the repository at this point in the history
* BatchingClientObjectFactory added to separate batching logic from HCHttp
* Batch and Item interfaces added (finally!) to handle API-specific implementations of "Batch domain"
* BatchOperations can now be configured with HCHttp.Builder (configurable provider of "Batch domain")
* HCHttp.Builder.pooledItemSourceFactory deprecated
* HCHttp.itemSourceFactory removed
* HCHttp.Builder.mappingType deprecated
* HCHttp.mappingType removed
* OperationFactory MUST be provided
  • Loading branch information
rfoltyns committed Feb 18, 2021
1 parent d19e685 commit 5016080
Show file tree
Hide file tree
Showing 23 changed files with 1,557 additions and 871 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.appenders.log4j2.elasticsearch;

/*-
* #%L
* log4j2-elasticsearch
* %%
* Copyright (C) 2021 Rafal Foltynski
* %%
* Licensed 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.
* #L%
*/

import java.util.function.Function;

public interface BatchListenerFactory<BATCH_TYPE> {

/**
* Listener that MUST accept and send prepared batch and handle the exceptions
* @param failoverPolicy sink for failed batch items
* @return prepared batch handler
*/
Function<BATCH_TYPE, Boolean> createBatchListener(FailoverPolicy failoverPolicy);

/**
* Failed batch handler. SHOULD deliver the batch to alternate target or provided failover policy
* @param failover optional failover strategy
* @return prepared failed batch handler
*/
Function<BATCH_TYPE, Boolean> createFailureHandler(FailoverPolicy failover);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.appenders.log4j2.elasticsearch;

/*-
* #%L
* log4j2-elasticsearch
* %%
* Copyright (C) 2021 Rafal Foltynski
* %%
* Licensed 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.
* #L%
*/

import java.util.Collection;

public interface ClientFactory<CLIENT_TYPE> {

/**
* @return Collection of configured addresses
*/
Collection<String> getServerList();

/**
* @return CLIENT_TYPE Fully configured client
*/
CLIENT_TYPE createClient();

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
*/


import java.util.Collection;
import java.util.function.Function;

/**
* Factory for client-specific objects:
* <ul>
Expand All @@ -38,34 +35,10 @@
* @param <CLIENT_TYPE> type of client object produced by this factory
* @param <BATCH_TYPE> type of batch objects handled by the client
*/
public interface ClientObjectFactory<CLIENT_TYPE, BATCH_TYPE> extends LifeCycle {
public interface ClientObjectFactory<CLIENT_TYPE, BATCH_TYPE> extends LifeCycle, BatchListenerFactory<BATCH_TYPE>, ClientFactory<CLIENT_TYPE> {

String ELEMENT_TYPE = "objectFactory";

/**
* @return Collection of configured addresses
*/
Collection<String> getServerList();

/**
* @return CLIENT_TYPE Fully configured client
*/
CLIENT_TYPE createClient();

/**
* Listener that MUST accept and send prepared batch and handle the exceptions
* @param failoverPolicy sink for failed batch items
* @return prepared batch handler
*/
Function<BATCH_TYPE, Boolean> createBatchListener(FailoverPolicy failoverPolicy);

/**
* Failed batch handler. SHOULD deliver the batch to alternate target or provided failover policy
* @param failover optional failover strategy
* @return prepared failed batch handler
*/
Function<BATCH_TYPE, Boolean> createFailureHandler(FailoverPolicy failover);

/**
* @return batch items creator
*/
Expand All @@ -84,7 +57,9 @@ public interface ClientObjectFactory<CLIENT_TYPE, BATCH_TYPE> extends LifeCycle
*
* NOTE: {@code default} added for backwards compatibility. {@code default} will be removed future releases
* @param operation operation to be executed
* @deprecated As of 1.6, this method will be wrapped by batch-phase-based queue
*/
@Deprecated
default void addOperation(Operation operation) {}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.appenders.log4j2.elasticsearch.hc;

/*-
* #%L
* log4j2-elasticsearch
* %%
* Copyright (C) 2021 Rafal Foltynski
* %%
* Licensed 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.
* #L%
*/

import java.util.Collection;

public interface Batch<I> extends Request {
Collection<I> getItems();
void completed();
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* Allows to index multiple {@link org.appenders.log4j2.elasticsearch.hc.IndexRequest} documents
* in a single request.
*/
public class BatchRequest implements Request {
public class BatchRequest implements Batch<IndexRequest> {

public static final String HTTP_METHOD_NAME = "POST";
public static final char LINE_SEPARATOR = '\n';
Expand Down Expand Up @@ -121,10 +121,23 @@ public void completed() {

}

public Collection<IndexRequest> getIndexRequests() {
/**
* @return collection of batch items
*/
@Override
public Collection<IndexRequest> getItems() {
return indexRequests;
}

/**
* @return collection of batch items
* @deprecated As of 1.6, this method will be removed. Use {@link #getItems()} instead
*/
@Deprecated
public Collection<IndexRequest> getIndexRequests() {
return getItems();
}

@Override
public String getURI() {
return "/_bulk";
Expand Down
Loading

0 comments on commit 5016080

Please sign in to comment.