Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.aiven.kafka.connect.azure.source.utils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -30,18 +31,20 @@ public final class AzureBlobOffsetManagerEntry
public static final String CONTAINER = "container";
public static final String BLOB_NAME = "blobName";
public static final String RECORD_COUNT = "recordCount";
public static final String COMPLETED = "completed";

/**
* THe list of Keys that may not be set via {@link #setProperty(String, Object)}.
*/
static final List<String> RESTRICTED_KEYS = List.of(RECORD_COUNT);
static final List<String> RESTRICTED_KEYS = List.of(RECORD_COUNT, COMPLETED);
/** The data map that stores all the values */
private final Map<String, Object> data;
/** THe record count for the data map. Extracted here because it is used/updated frequently during processing */
private long recordCount;

private final String container;
private final String blobName;
private boolean completed;

/**
* Construct the AzureOffsetManagerEntry.
Expand Down Expand Up @@ -72,6 +75,10 @@ private AzureBlobOffsetManagerEntry(final String container, final String blobNam
if (recordCountProperty instanceof Number) {
recordCount = ((Number) recordCountProperty).longValue();
}
final Object completedProperty = data.computeIfAbsent(COMPLETED, b -> false);
if (completedProperty instanceof Boolean) {
completed = (Boolean) completedProperty;
}
}

/**
Expand All @@ -83,7 +90,12 @@ private AzureBlobOffsetManagerEntry(final String container, final String blobNam
* @return a new instance of OffsetManagerKey
*/
public static OffsetManager.OffsetManagerKey asKey(final String bucket, final String blobName) {
return () -> Map.of(CONTAINER, bucket, BLOB_NAME, blobName);
return () -> {
final Map<String, Object> map = new LinkedHashMap<>();
map.put(CONTAINER, bucket);
map.put(BLOB_NAME, blobName);
return map;
};
}

/**
Expand All @@ -108,6 +120,9 @@ public Object getProperty(final String key) {
if (RECORD_COUNT.equals(key)) {
return recordCount;
}
if (COMPLETED.equals(key)) {
return completed;
}
return data.get(key);
}

Expand All @@ -125,6 +140,14 @@ public void incrementRecordCount() {
recordCount++;
}

/**
* Set the record to be completed
*/
@Override
public void setComplete() {
completed = true;
}

/**
* Gets the umber of records extracted from data returned from Azure Blob.
*
Expand All @@ -135,6 +158,16 @@ public long getRecordCount() {
return recordCount;
}

/**
* Gets the umber of records extracted from data returned from Azure.
*
* @return the umber of records extracted from data returned from Azure.
*/
@Override
public boolean isComplete() {
return completed;
}

/**
* Gets the blob name for the current object.
*
Expand All @@ -161,6 +194,7 @@ public String getContainer() {
public Map<String, Object> getProperties() {
final Map<String, Object> result = new HashMap<>(data);
result.put(RECORD_COUNT, recordCount);
result.put(COMPLETED, completed);
return result;
}
/**
Expand All @@ -170,7 +204,12 @@ public Map<String, Object> getProperties() {
*/
@Override
public OffsetManager.OffsetManagerKey getManagerKey() {
return () -> Map.of(CONTAINER, container, BLOB_NAME, blobName);
return () -> {
final Map<String, Object> map = new LinkedHashMap<>();
map.put(CONTAINER, container);
map.put(BLOB_NAME, blobName);
return map;
};
}

@Override
Expand All @@ -197,6 +236,9 @@ public int compareTo(final AzureBlobOffsetManagerEntry other) {
if (result == 0) {
result = Long.compare(getRecordCount(), other.getRecordCount());
}
if (result == 0) {
result = Boolean.compare(isComplete(), other.isComplete());
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,21 @@ void testFromProperties() {
assertThat(other.getProperty("random_entry")).isEqualTo(5L);

}

@Test
void testSetAndGetCompleted() {
final AzureBlobOffsetManagerEntry entry = new AzureBlobOffsetManagerEntry(TEST_CONTAINER, BLOB_NAME);
assertThat(entry.getRecordCount()).isEqualTo(0L);
assertThat(entry.getProperty("random_entry")).isNull();
assertThat(entry.isComplete()).isFalse();

entry.setProperty("random_entry", 5L);
entry.incrementRecordCount();
entry.setComplete();

assertThat(entry.getRecordCount()).isEqualTo(1L);
assertThat(entry.getProperty("random_entry")).isEqualTo(5L);
assertThat(entry.isComplete()).isTrue();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
final public class AzureBlobSourceRecordIteratorTest
extends
AbstractSourceRecordIteratorTest<String, BlobItem, AzureBlobOffsetManagerEntry, AzureBlobSourceRecord> { // NOPMD
// TestClassWithoutTestCases
// TestClassWithoutTestCases
public static final String TEST_CONTAINER = "container1";

private AzureBlobClient azureBlobClient;

Expand All @@ -59,10 +60,15 @@ protected AbstractSourceRecordIterator<String, BlobItem, AzureBlobOffsetManagerE
return new Mutator();
}

@Override
protected OffsetManager.OffsetManagerKey createKey(final String key) {
return AzureBlobOffsetManagerEntry.asKey(TEST_CONTAINER, key);
}

@Override
protected SourceCommonConfig createMockedConfig() {
final AzureBlobSourceConfig config = mock(AzureBlobSourceConfig.class);
when(config.getAzureContainerName()).thenReturn("container1");
when(config.getAzureContainerName()).thenReturn(TEST_CONTAINER);
return config;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.InputStream;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -224,7 +225,15 @@ final public boolean hasNext() {

@Override
final public T next() {
return outer.next();
// retrieve the next transformed record.
final T result = outer.next();
// if it was the last record then set the entry as complete.
if (!outer.hasNext()) {
final O ome = result.getOffsetManagerEntry();
ome.setComplete();
result.setOffsetManagerEntry(ome);
}
return result;
}

@Override
Expand Down Expand Up @@ -345,6 +354,10 @@ class FileMatching implements Function<N, Optional<T>> {
@Override
public Optional<T> apply(final N nativeItem) {
final K itemName = getNativeKey(nativeItem);
final OffsetManager.OffsetManagerKey key = getOffsetManagerKey(itemName);
if (key != null) {
offsetManager.populateOffsetManager(List.of(key));
}
final Optional<Context<K>> optionalContext = utils.process(itemName);
if (optionalContext.isPresent() && !ringBuffer.contains(itemName)) {
final T sourceRecord = createSourceRecord(nativeItem);
Expand All @@ -356,6 +369,11 @@ public Optional<T> apply(final N nativeItem) {
.getEntry(offsetManagerEntry.getManagerKey(), offsetManagerEntry::fromProperties)
.orElse(offsetManagerEntry);
sourceRecord.setOffsetManagerEntry(offsetManagerEntry);
if (offsetManagerEntry.isComplete()) {
getLogger().debug("Skipping entry {} as entry is already marked as complete : {}", nativeItem,
offsetManagerEntry.isComplete());
return Optional.empty();
}
return Optional.of(sourceRecord);
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,25 @@ default String getString(final String key) {
*/
void incrementRecordCount();

/**
* Set the record as completed
*/
void setComplete();

/**
* Gets the current record count.
*
* @return The current record count.
*/
long getRecordCount();

/**
* Gets the completion status of the data source
*
* @return a boolean value indicating if the entire data source was processed for this offset, true of fully
* processed, false if not fully processed
*/
boolean isComplete();
Comment thread
aindriu-aiven marked this conversation as resolved.
Resolved
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.aiven.kafka.connect.common.source.impl;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import io.aiven.kafka.connect.common.source.OffsetManager;
Expand All @@ -34,6 +35,8 @@ public class ExampleOffsetManagerEntry implements OffsetManager.OffsetManagerEnt
private static final String KEY = "key";
private static final String GROUPING_KEY = "groupingKey";
private static final String RECORD_COUNT = "recordCount";
public static final String COMPLETED = "completed";
private boolean completed;

/**
* Constructor.
Expand All @@ -48,6 +51,10 @@ public ExampleOffsetManagerEntry(final String nativeKey, final String grouping)
this();
data.put(KEY, nativeKey);
data.put(GROUPING_KEY, grouping);
final Object completedProperty = data.computeIfAbsent(COMPLETED, b -> false);
if (completedProperty instanceof Boolean) {
completed = (Boolean) completedProperty;
}
}

/**
Expand All @@ -69,6 +76,9 @@ public ExampleOffsetManagerEntry(final Map<String, Object> properties) {
if (data.containsKey(RECORD_COUNT)) {
recordCount = getInt(RECORD_COUNT);
}
if (data.containsKey(COMPLETED)) {
completed = (boolean) data.get(COMPLETED);
}
}

@Override
Expand All @@ -79,9 +89,27 @@ public ExampleOffsetManagerEntry fromProperties(final Map<String, Object> proper
@Override
public Map<String, Object> getProperties() {
data.put(RECORD_COUNT, recordCount);
data.put(COMPLETED, completed);
return data;
}

/**
*
* @param groupingKey
* the bucket we are using.
* @param key
* the S3Object key.
* @return a new instance of OffsetManagerKey
*/
public static OffsetManager.OffsetManagerKey asKey(final String groupingKey, final String key) {
return () -> {
final Map<String, Object> map = new LinkedHashMap<>();
map.put(GROUPING_KEY, groupingKey);
map.put(KEY, key);
return map;
};
}

@Override
public Object getProperty(final String key) {
return data.get(key);
Expand All @@ -102,11 +130,21 @@ public void incrementRecordCount() {
recordCount++;
}

@Override
public void setComplete() {
completed = true;
}

@Override
public long getRecordCount() {
return recordCount;
}

@Override
public boolean isComplete() {
return completed;
}

@Override
public boolean equals(final Object other) {
if (other instanceof ExampleOffsetManagerEntry) {
Expand All @@ -131,6 +169,9 @@ public int compareTo(final ExampleOffsetManagerEntry other) {
if (result == 0) {
result = Long.compare(getRecordCount(), other.getRecordCount());
}
if (result == 0) {
result = Boolean.compare(isComplete(), other.isComplete());
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ protected SourceCommonConfig createMockedConfig() {
return mock(SourceCommonConfig.class);
}

/**
* Create the offsetManager key that matches the offset entry
*
* @param key
* The string value of the key that identifies the object
* @return OffsetManagerKey
*/
@Override
protected OffsetManager.OffsetManagerKey createKey(final String key) {
return ExampleOffsetManagerEntry.asKey("GROUPING_KEY", key);
}

@SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "stores mutable fields in offset manager to be reviewed before release")
public class Mutator extends ClientMutator<ExampleNativeObject, String, Mutator> {

Expand Down
Loading
Loading