Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kinesis adaptive memory management #15360

Merged
merged 31 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
85f25ed
* do stuff
zachjsh Oct 31, 2023
013b9d7
* move existing MemoryBoundLinkedBlockingQueue to druid core and exte…
zachjsh Nov 10, 2023
39545aa
* fix configs, tests
zachjsh Nov 14, 2023
ff77302
* add tests
zachjsh Nov 14, 2023
97a7ae3
Merge remote-tracking branch 'apache/master' into kinesis-adaptive-me…
zachjsh Nov 14, 2023
7b155f6
* fix serde
zachjsh Nov 14, 2023
74e0ad2
* minor fix to logging
zachjsh Nov 14, 2023
9547251
* update comment
zachjsh Nov 14, 2023
d7f9c26
* remove references to removed config properties from documentation a…
zachjsh Nov 15, 2023
fd57dfb
* fix spellcheck
zachjsh Nov 16, 2023
6666bc8
* dont throw away the rest of the GetRecords result after recordBuff…
zachjsh Nov 17, 2023
8bd7c69
* address review comments
zachjsh Nov 27, 2023
f3bac06
Merge remote-tracking branch 'apache/master' into kinesis-adaptive-me…
zachjsh Nov 27, 2023
b6349fb
* fix integration test compilation failure
zachjsh Nov 27, 2023
0300074
* fix code scan failure
zachjsh Nov 28, 2023
f4c0665
* review comments
zachjsh Nov 29, 2023
a4b3b35
Merge remote-tracking branch 'apache/master' into kinesis-adaptive-me…
zachjsh Nov 29, 2023
bca26e9
* fix ingestion-spec.tsx
zachjsh Nov 29, 2023
127bf0d
* change back to ArrayList
zachjsh Nov 29, 2023
07840a7
* fix ingestion-spec.tsx again
zachjsh Nov 29, 2023
0287eb1
* more review comments
zachjsh Dec 1, 2023
329a4d6
Merge remote-tracking branch 'apache/master' into kinesis-adaptive-me…
zachjsh Dec 1, 2023
1a6d83a
* make blocking queue actually block for time specified
zachjsh Dec 15, 2023
bce0530
* fix checkstyle
zachjsh Dec 15, 2023
047c266
* fix failing test
zachjsh Dec 15, 2023
765efc4
* signal not empty
zachjsh Dec 15, 2023
30e0148
* fix test
zachjsh Dec 18, 2023
5e5a84e
* fix test finally
zachjsh Dec 18, 2023
48425e4
Merge remote-tracking branch 'apache/master' into kinesis-adaptive-me…
zachjsh Jan 11, 2024
40f4f9e
Merge remote-tracking branch 'apache/master' into kinesis-adaptive-me…
zachjsh Jan 17, 2024
cb1fe85
Merge remote-tracking branch 'apache/master' into kinesis-adaptive-me…
zachjsh Jan 19, 2024
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
26 changes: 10 additions & 16 deletions docs/development/extensions-core/kinesis-ingestion.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,9 @@ The following table outlines the configuration options for `ioConfig`:
|`completionTimeout`|ISO 8601 period|The length of time to wait before Druid declares a publishing task has failed and terminates it. If this is set too low, your tasks may never publish. The publishing clock for a task begins roughly after `taskDuration` elapses.|No|PT6H|
|`lateMessageRejectionPeriod`|ISO 8601 period|Configure tasks to reject messages with timestamps earlier than this period before the task is created. For example, if `lateMessageRejectionPeriod` is set to `PT1H` and the supervisor creates a task at `2016-01-01T12:00Z`, messages with timestamps earlier than `2016-01-01T11:00Z` are dropped. This may help prevent concurrency issues if your data stream has late messages and you have multiple pipelines that need to operate on the same segments, such as a streaming and a nightly batch ingestion pipeline.|No||
|`earlyMessageRejectionPeriod`|ISO 8601 period|Configure tasks to reject messages with timestamps later than this period after the task reached its `taskDuration`. For example, if `earlyMessageRejectionPeriod` is set to `PT1H`, the `taskDuration` is set to `PT1H` and the supervisor creates a task at `2016-01-01T12:00Z`. Messages with timestamps later than `2016-01-01T14:00Z` are dropped. **Note:** Tasks sometimes run past their task duration, for example, in cases of supervisor failover. Setting `earlyMessageRejectionPeriod` too low may cause messages to be dropped unexpectedly whenever a task runs past its originally configured task duration.|No||
|`recordsPerFetch`|Integer|The number of records to request per call to fetch records from Kinesis.|No| See [Determine fetch settings](#determine-fetch-settings) for defaults.|
|`fetchDelayMillis`|Integer|Time in milliseconds to wait between subsequent calls to fetch records from Kinesis. See [Determine fetch settings](#determine-fetch-settings).|No|0|
|`awsAssumedRoleArn`|String|The AWS assumed role to use for additional permissions.|No||
|`awsExternalId`|String|The AWS external ID to use for additional permissions.|No||
|`deaggregate`|Boolean|Whether to use the deaggregate function of the Kinesis Client Library (KCL).|No||
|`autoScalerConfig`|Object|Defines autoscaling behavior for Kinesis ingest tasks. See [Task autoscaler properties](#task-autoscaler-properties) for more information.|No|null|

### Task autoscaler properties
Expand Down Expand Up @@ -406,7 +404,7 @@ The following table outlines the configuration options for `tuningConfig`:
|`chatRetries`|Integer|The number of times Druid retries HTTP requests to indexing tasks before considering tasks unresponsive.|No|8|
|`httpTimeout`|ISO 8601 period|The period of time to wait for a HTTP response from an indexing task.|No|PT10S|
|`shutdownTimeout`|ISO 8601 period|The period of time to wait for the supervisor to attempt a graceful shutdown of tasks before exiting.|No|PT80S|
|`recordBufferSize`|Integer|The size of the buffer (number of events) Druid uses between the Kinesis fetch threads and the main ingestion thread.|No|See [Determine fetch settings](#determine-fetch-settings) for defaults.|
|`recordBufferSizeBytes`|Integer| The size of the buffer (heap memory bytes) Druid uses between the Kinesis fetch threads and the main ingestion thread. |No|See [Determine fetch settings](#determine-fetch-settings) for defaults.|
|`recordBufferOfferTimeout`|Integer|The number of milliseconds to wait for space to become available in the buffer before timing out.|No|5000|
|`recordBufferFullWait`|Integer|The number of milliseconds to wait for the buffer to drain before Druid attempts to fetch records from Kinesis again.|No|5000|
|`fetchThreads`|Integer|The size of the pool of threads fetching data from Kinesis. There is no benefit in having more threads than Kinesis shards.|No| `procs * 2`, where `procs` is the number of processors available to the task.|
Expand All @@ -419,6 +417,7 @@ The following table outlines the configuration options for `tuningConfig`:
|`offsetFetchPeriod`|ISO 8601 period|Determines how often the supervisor queries Kinesis and the indexing tasks to fetch current offsets and calculate lag. If the user-specified value is below the minimum value of PT5S, the supervisor ignores the value and uses the minimum value instead.|No|PT30S|
|`useListShards`|Boolean|Indicates if `listShards` API of AWS Kinesis SDK can be used to prevent `LimitExceededException` during ingestion. You must set the necessary `IAM` permissions.|No|`false`|


### IndexSpec

The following table outlines the configuration options for `indexSpec`:
Expand Down Expand Up @@ -656,25 +655,22 @@ For more detail, see [Segment size optimization](../../operations/segment-optimi

Kinesis indexing tasks fetch records using `fetchThreads` threads.
If `fetchThreads` is higher than the number of Kinesis shards, the excess threads are unused.
Each fetch thread fetches up to `recordsPerFetch` records at once from a Kinesis shard, with a delay between fetches
Each fetch thread fetches up to 10 MB of records at once from a Kinesis shard, with a delay between fetches
of `fetchDelayMillis`.
The records fetched by each thread are pushed into a shared queue of size `recordBufferSize`.
The records fetched by each thread are pushed into a shared queue of size `recordBufferSizeBytes`.
The main runner thread for each task polls up to `maxRecordsPerPoll` records from the queue at once.

When using Kinesis Producer Library's aggregation feature, that is when [`deaggregate`](#deaggregation) is set,
each of these parameters refers to aggregated records rather than individual records.

The default values for these parameters are:

- `fetchThreads`: Twice the number of processors available to the task. The number of processors available to the task
is the total number of processors on the server, divided by `druid.worker.capacity` (the number of task slots on that
particular server).
particular server). This value is further limited so that the total data record data fetched at a given time does not
exceed 5% of the max heap configured, assuming that each thread fetches 10 MB of records at once. If the value specified
for this configuration is higher than this limit, no failure occurs, but a warning is logged, and the value is
implicitly lowered to the max allowed by this constraint.
- `fetchDelayMillis`: 0 (no delay between fetches).
- `recordsPerFetch`: 100 MB or an estimated 5% of available heap, whichever is smaller, divided by `fetchThreads`.
For estimation purposes, Druid uses a figure of 10 KB for regular records and 1 MB for [aggregated records](#deaggregation).
- `recordBufferSize`: 100 MB or an estimated 10% of available heap, whichever is smaller.
For estimation purposes, Druid uses a figure of 10 KB for regular records and 1 MB for [aggregated records](#deaggregation).
- `maxRecordsPerPoll`: 100 for regular records, 1 for [aggregated records](#deaggregation).
- `recordBufferSizeBytes`: 100 MB or an estimated 10% of available heap, whichever is smaller.
- `maxRecordsPerPoll`: 1.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be higher? I wonder if this is too low in the case of non-aggregated records

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered the same actually. tbh, im not sure. I think validation for this requires extensive performance testing.

Copy link
Contributor Author

@zachjsh zachjsh Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it so that it polls for at least one record and at most 1_000_000 bytes if more than 1 record, which is what we were targeting for before.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So does that mean we should update the maxRecordsPerPoll: 1 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated


Kinesis places the following restrictions on calls to fetch records:

Expand All @@ -697,8 +693,6 @@ Kinesis stream.
The Kinesis indexing service supports de-aggregation of multiple rows packed into a single record by the Kinesis
Producer Library's aggregate method for more efficient data transfer.

To enable this feature, set `deaggregate` to true in your `ioConfig` when submitting a supervisor spec.

## Resharding

[Resharding](https://docs.aws.amazon.com/streams/latest/dev/kinesis-using-sdk-java-resharding.html) is an advanced operation that lets you adjust the number of shards in a stream to adapt to changes in the rate of data flowing through a stream.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.annotations.VisibleForTesting;
import org.apache.druid.emitter.kafka.KafkaEmitterConfig.EventType;
import org.apache.druid.emitter.kafka.MemoryBoundLinkedBlockingQueue.ObjectContainer;
import org.apache.druid.java.util.common.MemoryBoundLinkedBlockingQueue;
import org.apache.druid.java.util.common.StringUtils;
import org.apache.druid.java.util.common.lifecycle.LifecycleStop;
import org.apache.druid.java.util.common.logger.Logger;
Expand Down Expand Up @@ -173,7 +173,7 @@ private void sendSegmentMetadataToKafka()

private void sendToKafka(final String topic, MemoryBoundLinkedBlockingQueue<String> recordQueue, Callback callback)
{
ObjectContainer<String> objectToSend;
MemoryBoundLinkedBlockingQueue.ObjectContainer<String> objectToSend;
try {
while (true) {
objectToSend = recordQueue.take();
Expand All @@ -199,7 +199,7 @@ public void emit(final Event event)

String resultJson = jsonMapper.writeValueAsString(map);

ObjectContainer<String> objectContainer = new ObjectContainer<>(
MemoryBoundLinkedBlockingQueue.ObjectContainer<String> objectContainer = new MemoryBoundLinkedBlockingQueue.ObjectContainer<>(
resultJson,
StringUtils.toUtf8(resultJson).length
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
public class KinesisIndexTask extends SeekableStreamIndexTask<String, String, ByteEntity>
{
private static final String TYPE = "index_kinesis";

// GetRecords returns maximum 10MB per call
// (https://docs.aws.amazon.com/streams/latest/dev/service-sizes-and-limits.html)
private static final long GET_RECORDS_MAX_BYTES_PER_CALL = 10_000_000L;
private static final Logger log = new Logger(KinesisIndexTask.class);

private final boolean useListShards;
Expand Down Expand Up @@ -78,6 +82,10 @@ public KinesisIndexTask(
);
this.useListShards = useListShards;
this.awsCredentialsConfig = awsCredentialsConfig;
if (tuningConfig.getRecordBufferSizeConfigured() != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move these two checks to run rather than the constructor, because we don't need to log this stuff every time a task object is constructed. (That happens at various points on the Overlord due to various API calls and internal machinations, and will create a log of log spam.)

Copy link
Contributor Author

@zachjsh zachjsh Dec 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Moved.

log.warn("The 'recordBufferSize' config property of the kinesis tuning config has been deprecated. "
+ "Please use 'recordBufferSizeBytes'.");
}
}

@Override
Expand Down Expand Up @@ -105,21 +113,18 @@ protected KinesisRecordSupplier newTaskRecordSupplier(final TaskToolbox toolbox)
{
KinesisIndexTaskIOConfig ioConfig = ((KinesisIndexTaskIOConfig) super.ioConfig);
KinesisIndexTaskTuningConfig tuningConfig = ((KinesisIndexTaskTuningConfig) super.tuningConfig);
final int recordBufferSizeBytes =
tuningConfig.getRecordBufferSizeBytesOrDefault(runtimeInfo.getMaxHeapSizeBytes());
final int fetchThreads = computeFetchThreads(runtimeInfo, tuningConfig.getFetchThreads());
final int recordsPerFetch = ioConfig.getRecordsPerFetchOrDefault(runtimeInfo.getMaxHeapSizeBytes(), fetchThreads);
final int recordBufferSize =
tuningConfig.getRecordBufferSizeOrDefault(runtimeInfo.getMaxHeapSizeBytes(), ioConfig.isDeaggregate());
final int maxRecordsPerPoll = tuningConfig.getMaxRecordsPerPollOrDefault(ioConfig.isDeaggregate());
final int maxRecordsPerPoll = tuningConfig.getMaxRecordsPerPollOrDefault();

log.info(
"Starting record supplier with fetchThreads [%d], fetchDelayMillis [%d], recordsPerFetch [%d], "
+ "recordBufferSize [%d], maxRecordsPerPoll [%d], deaggregate [%s].",
"Starting record supplier with fetchThreads [%d], fetchDelayMillis [%d], "
+ "recordBufferSizeBytes [%d], maxRecordsPerPoll [%d]",
fetchThreads,
ioConfig.getFetchDelayMillis(),
recordsPerFetch,
recordBufferSize,
maxRecordsPerPoll,
ioConfig.isDeaggregate()
recordBufferSizeBytes,
maxRecordsPerPoll
);

return new KinesisRecordSupplier(
Expand All @@ -129,11 +134,9 @@ protected KinesisRecordSupplier newTaskRecordSupplier(final TaskToolbox toolbox)
ioConfig.getAwsAssumedRoleArn(),
ioConfig.getAwsExternalId()
),
recordsPerFetch,
ioConfig.getFetchDelayMillis(),
fetchThreads,
ioConfig.isDeaggregate(),
recordBufferSize,
recordBufferSizeBytes,
tuningConfig.getRecordBufferOfferTimeout(),
tuningConfig.getRecordBufferFullWait(),
maxRecordsPerPoll,
Expand Down Expand Up @@ -179,15 +182,36 @@ AWSCredentialsConfig getAwsCredentialsConfig()
}

@VisibleForTesting
static int computeFetchThreads(final RuntimeInfo runtimeInfo, final Integer configuredFetchThreads)
static int computeFetchThreads(
final RuntimeInfo runtimeInfo,
final Integer configuredFetchThreads
)
{
final int fetchThreads;
int fetchThreads;
if (configuredFetchThreads != null) {
fetchThreads = configuredFetchThreads;
} else {
fetchThreads = runtimeInfo.getAvailableProcessors() * 2;
}

// Each fetchThread can return upto 10MB at a time
// (https://docs.aws.amazon.com/streams/latest/dev/service-sizes-and-limits.html), cap fetchThreads so that
// we don't exceed more than the least of 100MB or 5% of heap at a time. Don't fail if fetchThreads specified
// is greater than this as to not cause failure for older configurations, but log warning in this case, and lower
// fetchThreads implicitly.
final long memoryToUse = Math.min(
KinesisIndexTaskIOConfig.MAX_RECORD_FETCH_MEMORY,
(long) (runtimeInfo.getMaxHeapSizeBytes() * KinesisIndexTaskIOConfig.RECORD_FETCH_MEMORY_MAX_HEAP_FRACTION)
);
int maxFetchThreads = Math.max(
1,
(int) (memoryToUse / GET_RECORDS_MAX_BYTES_PER_CALL)
);
if (fetchThreads > maxFetchThreads) {
log.warn("fetchThreads [%d] being lowered to [%d]", fetchThreads, maxFetchThreads);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This warning should only get logged if configuredFetchThreads != null. There's no reason to log it if runtimeInfo.getAvailableProcessors() * 2 is lower than maxFetchThreads.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, updated.

fetchThreads = maxFetchThreads;
}

Preconditions.checkArgument(
fetchThreads > 0,
"Must have at least one background fetch thread for the record supplier"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import com.google.common.primitives.Ints;
import org.apache.druid.data.input.InputFormat;
import org.apache.druid.indexing.seekablestream.SeekableStreamEndSequenceNumbers;
import org.apache.druid.indexing.seekablestream.SeekableStreamIndexTaskIOConfig;
Expand All @@ -41,21 +40,19 @@ public class KinesisIndexTaskIOConfig extends SeekableStreamIndexTaskIOConfig<St
* Together with {@link KinesisIndexTaskTuningConfig#MAX_RECORD_BUFFER_MEMORY}, don't take up more than 200MB
* per task.
*/
private static final int MAX_RECORD_FETCH_MEMORY = 100_000_000;
public static final int MAX_RECORD_FETCH_MEMORY = 100_000_000;

/**
* Together with {@link KinesisIndexTaskTuningConfig#RECORD_BUFFER_MEMORY_MAX_HEAP_FRACTION}, don't take up more
* than 15% of the heap.
*/
private static final double RECORD_FETCH_MEMORY_MAX_HEAP_FRACTION = 0.05;
public static final double RECORD_FETCH_MEMORY_MAX_HEAP_FRACTION = 0.05;

private final String endpoint;
private final Integer recordsPerFetch;
private final int fetchDelayMillis;

private final String awsAssumedRoleArn;
private final String awsExternalId;
private final boolean deaggregate;

@JsonCreator
public KinesisIndexTaskIOConfig(
Expand All @@ -79,11 +76,9 @@ public KinesisIndexTaskIOConfig(
@JsonProperty("maximumMessageTime") DateTime maximumMessageTime,
@JsonProperty("inputFormat") @Nullable InputFormat inputFormat,
@JsonProperty("endpoint") String endpoint,
@JsonProperty("recordsPerFetch") Integer recordsPerFetch,
@JsonProperty("fetchDelayMillis") Integer fetchDelayMillis,
@JsonProperty("awsAssumedRoleArn") String awsAssumedRoleArn,
@JsonProperty("awsExternalId") String awsExternalId,
@JsonProperty("deaggregate") boolean deaggregate
@JsonProperty("awsExternalId") String awsExternalId
)
{
super(
Expand All @@ -105,11 +100,9 @@ public KinesisIndexTaskIOConfig(
);

this.endpoint = Preconditions.checkNotNull(endpoint, "endpoint");
this.recordsPerFetch = recordsPerFetch;
this.fetchDelayMillis = fetchDelayMillis != null ? fetchDelayMillis : DEFAULT_FETCH_DELAY_MILLIS;
this.awsAssumedRoleArn = awsAssumedRoleArn;
this.awsExternalId = awsExternalId;
this.deaggregate = deaggregate;
}

public KinesisIndexTaskIOConfig(
Expand All @@ -122,11 +115,9 @@ public KinesisIndexTaskIOConfig(
DateTime maximumMessageTime,
InputFormat inputFormat,
String endpoint,
Integer recordsPerFetch,
Integer fetchDelayMillis,
String awsAssumedRoleArn,
String awsExternalId,
boolean deaggregate
String awsExternalId
)
{
this(
Expand All @@ -142,11 +133,9 @@ public KinesisIndexTaskIOConfig(
maximumMessageTime,
inputFormat,
endpoint,
recordsPerFetch,
fetchDelayMillis,
awsAssumedRoleArn,
awsExternalId,
deaggregate
awsExternalId
);
}

Expand Down Expand Up @@ -215,32 +204,6 @@ public String getEndpoint()
return endpoint;
}

@Nullable
@JsonProperty("recordsPerFetch")
@JsonInclude(JsonInclude.Include.NON_NULL)
public Integer getRecordsPerFetchConfigured()
{
return recordsPerFetch;
}

public int getRecordsPerFetchOrDefault(final long maxHeapSize, final int fetchThreads)
{
if (recordsPerFetch != null) {
return recordsPerFetch;
} else {
final long memoryToUse = Math.min(
MAX_RECORD_FETCH_MEMORY,
(long) (maxHeapSize * RECORD_FETCH_MEMORY_MAX_HEAP_FRACTION)
);

final int assumedRecordSize = deaggregate
? KinesisIndexTaskTuningConfig.ASSUMED_RECORD_SIZE_AGGREGATE
: KinesisIndexTaskTuningConfig.ASSUMED_RECORD_SIZE;

return Ints.checkedCast(Math.max(1, memoryToUse / assumedRecordSize / fetchThreads));
}
}

@JsonProperty
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public int getFetchDelayMillis()
Expand All @@ -262,13 +225,6 @@ public String getAwsExternalId()
return awsExternalId;
}

@JsonProperty
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public boolean isDeaggregate()
{
return deaggregate;
}

@Override
public String toString()
{
Expand All @@ -280,11 +236,9 @@ public String toString()
", minimumMessageTime=" + getMinimumMessageTime() +
", maximumMessageTime=" + getMaximumMessageTime() +
", endpoint='" + endpoint + '\'' +
", recordsPerFetch=" + recordsPerFetch +
", fetchDelayMillis=" + fetchDelayMillis +
", awsAssumedRoleArn='" + awsAssumedRoleArn + '\'' +
", awsExternalId='" + awsExternalId + '\'' +
", deaggregate=" + deaggregate +
'}';
}
}
Loading