-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[Feature][Connectors-v2] Support auto-increment id for FakeSource #9505
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
Changes from all commits
766b07e
e66ae40
26a3c55
ac9f9ac
3e1b358
fc05086
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * 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.seatunnel.connectors.seatunnel.fake.utils; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
| public class AutoIncrementIdGenerator implements Serializable { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| private final AtomicLong id; | ||
|
|
||
| public AutoIncrementIdGenerator(long start) { | ||
| this.id = new AtomicLong(start); | ||
| } | ||
|
|
||
| public Long getNextId() { | ||
| return id.getAndIncrement(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -38,9 +38,11 @@ | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public class FakeDataRandomUtils { | ||||||||||||||||||||||||
| private final FakeConfig fakeConfig; | ||||||||||||||||||||||||
| private final String jobId; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public FakeDataRandomUtils(FakeConfig fakeConfig) { | ||||||||||||||||||||||||
| public FakeDataRandomUtils(FakeConfig fakeConfig, String jobId) { | ||||||||||||||||||||||||
| this.fakeConfig = fakeConfig; | ||||||||||||||||||||||||
| this.jobId = jobId; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private static <T> T randomFromList(List<T> list) { | ||||||||||||||||||||||||
|
|
@@ -93,6 +95,22 @@ public Short randomSmallint(Column column) { | |||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public Integer randomInt(Column column) { | ||||||||||||||||||||||||
| if (fakeConfig.getAutoIncrementEnabled() | ||||||||||||||||||||||||
| && IdGeneratorUtils.isPrimaryColumn(fakeConfig, column.getName())) { | ||||||||||||||||||||||||
| if (fakeConfig.getAutoIncrementStart() | ||||||||||||||||||||||||
| + ((long) fakeConfig.getParallelism() * fakeConfig.getRowNum()) | ||||||||||||||||||||||||
| > Integer.MAX_VALUE) { | ||||||||||||||||||||||||
| throw new IllegalArgumentException( | ||||||||||||||||||||||||
| "The auto increment start value is too large, please check your configuration."); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| return IdGeneratorUtils.getIdGenerator(jobId, fakeConfig, column.getName()) | ||||||||||||||||||||||||
| .orElseThrow( | ||||||||||||||||||||||||
| () -> | ||||||||||||||||||||||||
| new IllegalArgumentException( | ||||||||||||||||||||||||
| "Auto increment is enabled, but no id generator found.")) | ||||||||||||||||||||||||
| .getNextId() | ||||||||||||||||||||||||
| .intValue(); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| List<Integer> intTemplate = fakeConfig.getIntTemplate(); | ||||||||||||||||||||||||
| if (!CollectionUtils.isEmpty(intTemplate)) { | ||||||||||||||||||||||||
| return randomFromList(intTemplate); | ||||||||||||||||||||||||
|
|
@@ -101,6 +119,15 @@ public Integer randomInt(Column column) { | |||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public Long randomBigint(Column column) { | ||||||||||||||||||||||||
| if (fakeConfig.getAutoIncrementEnabled() | ||||||||||||||||||||||||
| && IdGeneratorUtils.isPrimaryColumn(fakeConfig, column.getName())) { | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
| && IdGeneratorUtils.isPrimaryColumn(fakeConfig, column.getName())) { | |
| && IdGeneratorUtils.isPrimaryColumn(fakeConfig, column.getName())) { | |
| long autoIncrementStart = fakeConfig.getAutoIncrementStart(); | |
| long parallelism = fakeConfig.getParallelism(); | |
| long rowNum = fakeConfig.getRowNum(); | |
| if (autoIncrementStart > 0 && parallelism > 0 && rowNum > 0) { | |
| if (autoIncrementStart > Long.MAX_VALUE - (parallelism * rowNum)) { | |
| throw new IllegalArgumentException( | |
| "The auto increment start value is too large, or the combination of parallelism and rowNum causes an overflow. Please check your configuration."); | |
| } | |
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,72 @@ | ||||||
| /* | ||||||
| * 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.seatunnel.connectors.seatunnel.fake.utils; | ||||||
|
|
||||||
| import org.apache.seatunnel.shade.com.google.common.cache.Cache; | ||||||
| import org.apache.seatunnel.shade.com.google.common.cache.CacheBuilder; | ||||||
|
|
||||||
| import org.apache.seatunnel.api.table.catalog.CatalogTable; | ||||||
| import org.apache.seatunnel.api.table.catalog.PrimaryKey; | ||||||
| import org.apache.seatunnel.connectors.seatunnel.fake.config.FakeConfig; | ||||||
|
|
||||||
| import java.util.List; | ||||||
| import java.util.Optional; | ||||||
| import java.util.concurrent.ExecutionException; | ||||||
| import java.util.concurrent.TimeUnit; | ||||||
|
|
||||||
| public class IdGeneratorUtils { | ||||||
|
|
||||||
| private static final Cache<String, AutoIncrementIdGenerator> idGenerators = | ||||||
| CacheBuilder.newBuilder() | ||||||
| .maximumSize(1000) | ||||||
| .expireAfterWrite(30, TimeUnit.MINUTES) | ||||||
| .build(); | ||||||
|
|
||||||
| public static synchronized Optional<AutoIncrementIdGenerator> getIdGenerator( | ||||||
|
||||||
| public static synchronized Optional<AutoIncrementIdGenerator> getIdGenerator( | |
| public static Optional<AutoIncrementIdGenerator> getIdGenerator( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The
@Slf4jannotation is added but no logging calls are used in this class; consider removing it or adding relevant log statements.