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

Add support for concurrent batch Append and Replace #14407

Merged
merged 56 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
d9563f2
Add support for concurrent batch Append and Replace
AmatyaAvadhanula Jun 12, 2023
35cc335
Merge remote-tracking branch 'upstream/master' into overlordSimulator
AmatyaAvadhanula Aug 1, 2023
4d01445
Fix compile errors
AmatyaAvadhanula Aug 2, 2023
5acf93b
Fix compilation in test
AmatyaAvadhanula Aug 2, 2023
be23936
Clean stray comments and move test tasks under test
AmatyaAvadhanula Aug 3, 2023
6a9e6e7
Address preliminary feedback
AmatyaAvadhanula Aug 7, 2023
e3e9cf3
Fix version logic
AmatyaAvadhanula Aug 7, 2023
2ed61fd
Fix checkstyle
AmatyaAvadhanula Aug 8, 2023
0d9b5e6
Merge remote-tracking branch 'upstream/master' into overlordSimulator
AmatyaAvadhanula Aug 16, 2023
38a0071
Fix test
AmatyaAvadhanula Aug 16, 2023
f5144a0
Handle lock type when not specified in context
AmatyaAvadhanula Aug 17, 2023
1413a30
Use Intervals.utc
AmatyaAvadhanula Aug 18, 2023
468e4a2
Fix merge conflicts
AmatyaAvadhanula Aug 18, 2023
d495c3c
Modify segment commit sql statement
AmatyaAvadhanula Aug 21, 2023
671c01c
Fix merge conflicts
AmatyaAvadhanula Aug 21, 2023
311d0ca
Clean up append and replace actions
kfaraz Aug 21, 2023
5e4876b
Merge branch 'overlordSimulator' of github.com:AmatyaAvadhanula/druid…
kfaraz Aug 21, 2023
a2732ca
Fix SQL, remove forbidden APIs
kfaraz Aug 21, 2023
8f1e165
Add unit tests
AmatyaAvadhanula Aug 22, 2023
83d9484
Resolve merge conflicts
AmatyaAvadhanula Aug 22, 2023
06cf8d3
Add comments
AmatyaAvadhanula Aug 22, 2023
5981130
Fix intellij inspections
AmatyaAvadhanula Aug 23, 2023
073bc26
Remove unnecessary exception handling
AmatyaAvadhanula Aug 23, 2023
12534ff
Merge remote-tracking branch 'upstream/master' into overlordSimulator
AmatyaAvadhanula Aug 23, 2023
b271f1f
Fix failing tests
AmatyaAvadhanula Aug 23, 2023
725265c
Add new test task class
AmatyaAvadhanula Aug 23, 2023
41c3cbe
A replacing task must look only at its own replace locks during commit
AmatyaAvadhanula Aug 23, 2023
1fabacf
Merge remote-tracking branch 'upstream/master' into overlordSimulator
AmatyaAvadhanula Aug 24, 2023
5963be3
Remove some unused fields
kfaraz Aug 25, 2023
9fd156c
Merge branch 'overlordSimulator' of github.com:AmatyaAvadhanula/druid…
kfaraz Aug 27, 2023
71023ca
Add some javadocs
kfaraz Aug 29, 2023
a1c22a8
Get tests running
kfaraz Sep 3, 2023
7b0e259
Fix IndexerStorageCoordinatorTest
kfaraz Sep 3, 2023
258caed
Merge branch 'master' of github.com:apache/druid into overlordSimulator
kfaraz Sep 4, 2023
2fd2b9e
Fix for upstream changes
kfaraz Sep 4, 2023
55eca90
Use supervisorTaskId inside ReplaceTaskLock
kfaraz Sep 4, 2023
1ba0e8d
Add CommandQueueTask, remove unneeded classes
kfaraz Sep 6, 2023
ff71674
Fix synchronization in CommandQueueTask
kfaraz Sep 6, 2023
4e0587f
Move some tests
kfaraz Sep 7, 2023
359a923
Add more tests
kfaraz Sep 11, 2023
2bb3b79
Add tests for multiple granularities
kfaraz Sep 19, 2023
ab0b400
Remove extra change
kfaraz Sep 19, 2023
8bb5a13
Fix tests
kfaraz Sep 20, 2023
ae5e7c4
Merge branch 'master' of github.com:apache/druid into overlordSimulator
kfaraz Sep 20, 2023
9c7d5b2
Remove unused dependency
kfaraz Sep 20, 2023
17ab844
Add more tests
kfaraz Sep 20, 2023
e2b04d4
Use correct init version for APPEND locks
kfaraz Sep 21, 2023
37640b7
Merge branch 'master' of github.com:apache/druid into overlordSimulator
kfaraz Sep 21, 2023
5f5c5bf
Fix IndexerSQLCoordinator tests
kfaraz Sep 22, 2023
444cfe4
Add tests for new TaskLocks utility methods
kfaraz Sep 23, 2023
6309702
Add tests for ReplaceTaskLock and SqlSegMetaManagerProvider
kfaraz Sep 23, 2023
f5b7092
Merge branch 'master' of github.com:apache/druid into overlordSimulator
kfaraz Sep 23, 2023
58433d0
Add CreateTablesTest, rename new table to upgradeSegments
kfaraz Sep 24, 2023
a88da61
Cleanup
kfaraz Sep 24, 2023
057252e
Fix spotted bugs
kfaraz Sep 24, 2023
7df51f2
Fix checkstyle
kfaraz Sep 24, 2023
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 @@ -37,19 +37,7 @@ public void testIsTransientException()
SQLServerConnector connector = new SQLServerConnector(
Suppliers.ofInstance(new MetadataStorageConnectorConfig()),
Suppliers.ofInstance(
new MetadataStorageTablesConfig(
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
)
MetadataStorageTablesConfig.fromBase(null)
)
);

Expand All @@ -70,7 +58,7 @@ public void testLimitClause()
SQLServerConnector connector = new SQLServerConnector(
Suppliers.ofInstance(new MetadataStorageConnectorConfig()),
Suppliers.ofInstance(
new MetadataStorageTablesConfig(null, null, null, null, null, null, null, null, null, null, null)
MetadataStorageTablesConfig.fromBase(null)
)
);
Assert.assertEquals("FETCH NEXT 100 ROWS ONLY", connector.limitClause(100));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public String getDriverClassName()
private static final Supplier<MetadataStorageConnectorConfig> CONNECTOR_CONFIG_SUPPLIER =
MetadataStorageConnectorConfig::new;
private static final Supplier<MetadataStorageTablesConfig> TABLES_CONFIG_SUPPLIER =
() -> new MetadataStorageTablesConfig(null, null, null, null, null, null, null, null, null, null, null);
() -> MetadataStorageTablesConfig.fromBase(null);


@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,7 @@ public void testIsTransientException()
{
PostgreSQLConnector connector = new PostgreSQLConnector(
Suppliers.ofInstance(new MetadataStorageConnectorConfig()),
Suppliers.ofInstance(
new MetadataStorageTablesConfig(
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
)
),
Suppliers.ofInstance(MetadataStorageTablesConfig.fromBase(null)),
new PostgreSQLConnectorConfig(),
new PostgreSQLTablesConfig()
);
Expand All @@ -68,9 +54,7 @@ public void testLimitClause()
{
PostgreSQLConnector connector = new PostgreSQLConnector(
Suppliers.ofInstance(new MetadataStorageConnectorConfig()),
Suppliers.ofInstance(
new MetadataStorageTablesConfig(null, null, null, null, null, null, null, null, null, null, null)
),
Suppliers.ofInstance(MetadataStorageTablesConfig.fromBase(null)),
new PostgreSQLConnectorConfig(),
new PostgreSQLTablesConfig()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public MetadataStorageTablesConfig getMetadataStorageTablesConfig()
null,
null,
null,
null,
segmentTable,
null,
null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
/*
* 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.druid.indexing.common.actions;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.ImmutableSet;
import org.apache.druid.indexing.common.TaskLock;
import org.apache.druid.indexing.common.task.IndexTaskUtils;
import org.apache.druid.indexing.common.task.Task;
import org.apache.druid.indexing.overlord.CriticalAction;
import org.apache.druid.indexing.overlord.DataSourceMetadata;
import org.apache.druid.indexing.overlord.SegmentPublishResult;
import org.apache.druid.indexing.overlord.TaskLockInfo;
import org.apache.druid.java.util.emitter.service.ServiceMetricEvent;
import org.apache.druid.query.DruidMetrics;
import org.apache.druid.segment.SegmentUtils;
import org.apache.druid.timeline.DataSegment;
import org.joda.time.Interval;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Append segments to metadata storage. The segment versions must all be less than or equal to a lock held by
* your task for the segment intervals.
AmatyaAvadhanula marked this conversation as resolved.
Show resolved Hide resolved
*/
public class SegmentTransactionalAppendAction implements TaskAction<SegmentPublishResult>
{
private final Set<DataSegment> segments;

@Nullable
private final DataSourceMetadata startMetadata;
@Nullable
private final DataSourceMetadata endMetadata;
@Nullable
private final String dataSource;
kfaraz marked this conversation as resolved.
Show resolved Hide resolved

public static SegmentTransactionalAppendAction appendAction(
Set<DataSegment> segments,
@Nullable DataSourceMetadata startMetadata,
@Nullable DataSourceMetadata endMetadata
)
{
return new SegmentTransactionalAppendAction(segments, startMetadata, endMetadata, null);
}

@JsonCreator
private SegmentTransactionalAppendAction(
@JsonProperty("segments") @Nullable Set<DataSegment> segments,
@JsonProperty("startMetadata") @Nullable DataSourceMetadata startMetadata,
@JsonProperty("endMetadata") @Nullable DataSourceMetadata endMetadata,
@JsonProperty("dataSource") @Nullable String dataSource
)
{
this.segments = segments == null ? ImmutableSet.of() : ImmutableSet.copyOf(segments);
this.startMetadata = startMetadata;
this.endMetadata = endMetadata;
this.dataSource = dataSource;
}

@JsonProperty
public Set<DataSegment> getSegments()
{
return segments;
}

@JsonProperty
@Nullable
public DataSourceMetadata getStartMetadata()
{
return startMetadata;
}

@JsonProperty
@Nullable
public DataSourceMetadata getEndMetadata()
{
return endMetadata;
}

@JsonProperty
@Nullable
public String getDataSource()
{
return dataSource;
}

@Override
public TypeReference<SegmentPublishResult> getReturnTypeReference()
{
return new TypeReference<SegmentPublishResult>()
{
};
}

/**
* Performs some sanity checks and publishes the given segments.
*/
@Override
public SegmentPublishResult perform(Task task, TaskActionToolbox toolbox)
{
final SegmentPublishResult retVal;

if (segments.isEmpty()) {
// A stream ingestion task didn't ingest any rows and created no segments (e.g., all records were unparseable),
// but still needs to update metadata with the progress that the task made.
try {
retVal = toolbox.getIndexerMetadataStorageCoordinator().commitMetadataOnly(
dataSource,
startMetadata,
endMetadata
);
}
catch (Exception e) {
throw new RuntimeException(e);
}
return retVal;
AmatyaAvadhanula marked this conversation as resolved.
Show resolved Hide resolved
}

final Set<DataSegment> allSegments = new HashSet<>(segments);
Copy link
Contributor

Choose a reason for hiding this comment

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

This Set appears to be getting made for the sole purpose of being iterated across in the critical section. I'm not sure why building yet-another set from the set that we already have is really helpful here?

I'm guessing that this is the result of code iterations, but please be very conscious of objects. No object should be created without a purpose.

Copy link
Contributor Author

@AmatyaAvadhanula AmatyaAvadhanula Aug 3, 2023

Choose a reason for hiding this comment

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

The set segments could be immutable but new segments can be added to allSegments


String datasource = task.getDataSource();
Map<Interval, TaskLock> replaceLocks = new HashMap<>();
for (TaskLock lock : TaskLocks.findReplaceLocksForSegments(datasource, toolbox.getTaskLockbox(), segments)) {
replaceLocks.put(lock.getInterval(), lock);
}
AmatyaAvadhanula marked this conversation as resolved.
Show resolved Hide resolved
Map<DataSegment, TaskLockInfo> appendSegmentLockMap = new HashMap<>();
Set<TaskLockInfo> taskLockInfos = new HashSet<>();
for (TaskLock taskLock : replaceLocks.values()) {
taskLockInfos.add(getTaskLockInfo(taskLock));
}

for (DataSegment segment : segments) {
Interval interval = segment.getInterval();
for (Interval key : replaceLocks.keySet()) {
if (key.contains(interval)) {
appendSegmentLockMap.put(segment, getTaskLockInfo(replaceLocks.get(key)));
}
}
}

try {
retVal = toolbox.getTaskLockbox().doInCriticalSection(
task,
allSegments.stream().map(DataSegment::getInterval).collect(Collectors.toList()),
kfaraz marked this conversation as resolved.
Show resolved Hide resolved
CriticalAction.<SegmentPublishResult>builder()
.onValidLocks(
() -> toolbox.getIndexerMetadataStorageCoordinator().announceHistoricalSegments(
kfaraz marked this conversation as resolved.
Show resolved Hide resolved
segments,
null,
startMetadata,
endMetadata,
appendSegmentLockMap,
taskLockInfos,
true
)
)
.onInvalidLocks(
() -> SegmentPublishResult.fail(
"Invalid task locks. Maybe they are revoked by a higher priority task."
+ " Please check the overlord log for details."
)
Comment on lines +98 to +101
Copy link
Contributor

Choose a reason for hiding this comment

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

I wish we could get a better error message than this... Ah well

Copy link
Contributor

Choose a reason for hiding this comment

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

We should have logged the intervals though.

)
.build()
);
}
catch (Exception e) {
throw new RuntimeException(e);
}

// Emit metrics
final ServiceMetricEvent.Builder metricBuilder = new ServiceMetricEvent.Builder();
IndexTaskUtils.setTaskDimensions(metricBuilder, task);

if (retVal.isSuccess()) {
toolbox.getEmitter().emit(metricBuilder.build("segment/txn/success", 1));
} else {
toolbox.getEmitter().emit(metricBuilder.build("segment/txn/failure", 1));
}

// getSegments() should return an empty set if announceHistoricalSegments() failed
Copy link
Contributor

Choose a reason for hiding this comment

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

If I understand this corectly, this comment is saying that the for loop is a noop if the critical section failed. I.e. this loop will only do something if retVal.isSuccess() == true. If that's the case, then let's just move the loop inside that part of the if/else above.

for (DataSegment segment : retVal.getSegments()) {
metricBuilder.setDimension(DruidMetrics.INTERVAL, segment.getInterval().toString());
metricBuilder.setDimension(
DruidMetrics.PARTITIONING_TYPE,
segment.getShardSpec() == null ? null : segment.getShardSpec().getType()
);
toolbox.getEmitter().emit(metricBuilder.build("segment/added/bytes", segment.getSize()));
}

return retVal;
}


private TaskLockInfo getTaskLockInfo(TaskLock taskLock)
{
return new TaskLockInfo(taskLock.getInterval(), taskLock.getVersion());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

the verb get makes it sound like youa re trying to "get" something. This is creating a new object, not getting anything. In the future, if you have a need for somethign like that (converting from one type to another), create a static creator method on the object that you are building. I.e. in this case, it would be TaskLockInfo.fromTaskLock(TaskLock lock)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TaskLock is not available in that package, which is why the class TaskLockInfo needs to be created there and this method cannot be implemented


@Override
public boolean isAudited()
{
return true;
}

@Override
public String toString()
{
return "SegmentTransactionalInsertAction{" +
"segments=" + SegmentUtils.commaSeparatedIdentifiers(segments) +
", startMetadata=" + startMetadata +
", endMetadata=" + endMetadata +
", dataSource='" + dataSource + '\'' +
'}';
}
}
Loading