Skip to content

Commit b3383db

Browse files
author
张文领
committed
Refactor auto-creating iceberg tags via ProcessFactory plugin
1 parent 6093954 commit b3383db

18 files changed

Lines changed: 130 additions & 152 deletions

File tree

‎amoro-ams/src/main/java/org/apache/amoro/server/AmoroManagementConf.java‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -174,24 +174,6 @@ public class AmoroManagementConf {
174174
.defaultValue(10)
175175
.withDescription("The number of threads used for refreshing tables.");
176176

177-
public static final ConfigOption<Boolean> AUTO_CREATE_TAGS_ENABLED =
178-
ConfigOptions.key("auto-create-tags.enabled")
179-
.booleanType()
180-
.defaultValue(true)
181-
.withDescription("Enable creating tags.");
182-
183-
public static final ConfigOption<Integer> AUTO_CREATE_TAGS_THREAD_COUNT =
184-
ConfigOptions.key("auto-create-tags.thread-count")
185-
.intType()
186-
.defaultValue(3)
187-
.withDescription("The number of threads used for creating tags.");
188-
189-
public static final ConfigOption<Duration> AUTO_CREATE_TAGS_INTERVAL =
190-
ConfigOptions.key("auto-create-tags.interval")
191-
.durationType()
192-
.defaultValue(Duration.ofMinutes(1))
193-
.withDescription("Interval for creating tags.");
194-
195177
public static final ConfigOption<Duration> REFRESH_TABLES_INTERVAL =
196178
ConfigOptions.key("refresh-tables.interval")
197179
.durationType()

‎amoro-ams/src/main/java/org/apache/amoro/server/AmoroServiceContainer.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ public void startOptimizingService() throws Exception {
294294
addHandlerChain(InlineTableExecutors.getInstance().getBlockerExpiringExecutor());
295295
addHandlerChain(InlineTableExecutors.getInstance().getHiveCommitSyncExecutor());
296296
addHandlerChain(InlineTableExecutors.getInstance().getTableRefreshingExecutor());
297-
addHandlerChain(InlineTableExecutors.getInstance().getTagsAutoCreatingExecutor());
298297
tableService.initialize();
299298
LOG.info("AMS table service have been initialized");
300299
tableManager.setTableService(tableService);

‎amoro-ams/src/main/java/org/apache/amoro/server/process/iceberg/IcebergProcessFactory.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ public class IcebergProcessFactory implements ProcessFactory {
7979
public static final ConfigOption<Duration> DATA_EXPIRE_INTERVAL =
8080
ConfigOptions.key("expire-data.interval").durationType().defaultValue(Duration.ofDays(1));
8181

82+
public static final ConfigOption<Boolean> AUTO_CREATE_TAG_ENABLED =
83+
ConfigOptions.key("auto-create-tags.enabled").booleanType().defaultValue(true);
84+
85+
public static final ConfigOption<Duration> AUTO_CREATE_TAG_INTERVAL =
86+
ConfigOptions.key("auto-create-tags.interval")
87+
.durationType()
88+
.defaultValue(Duration.ofMinutes(1));
89+
8290
private ExecuteEngine localEngine;
8391
private final Map<Action, ProcessTriggerStrategy> actions = Maps.newHashMap();
8492
private final List<TableFormat> formats =
@@ -119,6 +127,8 @@ public Optional<TableProcess> trigger(TableRuntime tableRuntime, Action action)
119127
return triggerCleanDanglingDelete(tableRuntime);
120128
} else if (IcebergActions.EXPIRE_DATA.equals(action)) {
121129
return triggerDataExpiring(tableRuntime);
130+
} else if (IcebergActions.AUTO_CREATE_TAGS.equals(action)) {
131+
return triggerAutoCreateTag(tableRuntime);
122132
}
123133

124134
return Optional.empty();
@@ -147,6 +157,8 @@ public TableProcess recover(TableRuntime tableRuntime, TableProcessStore store)
147157
return new DanglingDeleteFilesCleaningProcess(tableRuntime, localEngine);
148158
} else if (IcebergActions.EXPIRE_DATA.equals(action)) {
149159
return new DataExpiringProcess(tableRuntime, localEngine);
160+
} else if (IcebergActions.AUTO_CREATE_TAGS.equals(action)) {
161+
return new TagsAutoCreatingProcess(tableRuntime, localEngine);
150162
}
151163

152164
throw new RecoverProcessFailedException(
@@ -182,6 +194,12 @@ public void open(Map<String, String> properties) {
182194
this.actions.put(
183195
IcebergActions.EXPIRE_DATA, ProcessTriggerStrategy.triggerAtFixRate(interval));
184196
}
197+
198+
if (configs.getBoolean(AUTO_CREATE_TAG_ENABLED)) {
199+
Duration interval = configs.getDuration(AUTO_CREATE_TAG_INTERVAL);
200+
this.actions.put(
201+
IcebergActions.AUTO_CREATE_TAGS, ProcessTriggerStrategy.triggerAtFixRate(interval));
202+
}
185203
}
186204

187205
private Optional<TableProcess> triggerExpireSnapshot(TableRuntime tableRuntime) {
@@ -248,6 +266,16 @@ private Optional<TableProcess> triggerDataExpiring(TableRuntime tableRuntime) {
248266
return Optional.of(new DataExpiringProcess(tableRuntime, localEngine));
249267
}
250268

269+
private Optional<TableProcess> triggerAutoCreateTag(TableRuntime tableRuntime) {
270+
if (localEngine == null
271+
|| tableRuntime.getFormat() != TableFormat.ICEBERG
272+
|| !tableRuntime.getTableConfiguration().getTagConfiguration().isAutoCreateTag()) {
273+
return Optional.empty();
274+
}
275+
276+
return Optional.of(new TagsAutoCreatingProcess(tableRuntime, localEngine));
277+
}
278+
251279
@Override
252280
public void close() {}
253281

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.amoro.server.process.iceberg;
20+
21+
import org.apache.amoro.Action;
22+
import org.apache.amoro.AmoroTable;
23+
import org.apache.amoro.IcebergActions;
24+
import org.apache.amoro.TableRuntime;
25+
import org.apache.amoro.maintainer.TableMaintainer;
26+
import org.apache.amoro.process.ExecuteEngine;
27+
import org.apache.amoro.process.LocalProcess;
28+
import org.apache.amoro.process.TableProcess;
29+
import org.apache.amoro.server.optimizing.maintainer.TableMaintainerFactory;
30+
import org.apache.amoro.shade.guava32.com.google.common.collect.Maps;
31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
33+
34+
import java.util.Map;
35+
36+
/** Local table process for auto-creating Iceberg tags. */
37+
public class TagsAutoCreatingProcess extends TableProcess implements LocalProcess {
38+
39+
private static final Logger LOG = LoggerFactory.getLogger(TagsAutoCreatingProcess.class);
40+
41+
public TagsAutoCreatingProcess(TableRuntime tableRuntime, ExecuteEngine engine) {
42+
super(tableRuntime, engine);
43+
}
44+
45+
@Override
46+
public String tag() {
47+
return getAction().getName().toLowerCase();
48+
}
49+
50+
@Override
51+
public void run() {
52+
try {
53+
AmoroTable<?> amoroTable = tableRuntime.loadTable();
54+
TableMaintainer tableMaintainer = TableMaintainerFactory.create(amoroTable, tableRuntime);
55+
tableMaintainer.autoCreateTags();
56+
} catch (Throwable t) {
57+
LOG.error("Failed to create tags on {}", tableRuntime.getTableIdentifier(), t);
58+
throw new RuntimeException(t);
59+
}
60+
}
61+
62+
@Override
63+
public Action getAction() {
64+
return IcebergActions.AUTO_CREATE_TAGS;
65+
}
66+
67+
@Override
68+
public Map<String, String> getProcessParameters() {
69+
return Maps.newHashMap();
70+
}
71+
72+
@Override
73+
public Map<String, String> getSummary() {
74+
return Maps.newHashMap();
75+
}
76+
}

‎amoro-ams/src/main/java/org/apache/amoro/server/scheduler/inline/InlineTableExecutors.java‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public class InlineTableExecutors {
3232
private OptimizingCommitExecutor optimizingCommitExecutor;
3333
private ProcessDataExpiringExecutor processDataExpiringExecutor;
3434
private HiveCommitSyncExecutor hiveCommitSyncExecutor;
35-
private TagsAutoCreatingExecutor tagsAutoCreatingExecutor;
3635

3736
public static InlineTableExecutors getInstance() {
3837
return instance;
@@ -71,13 +70,6 @@ public void setup(TableService tableService, Configurations conf) {
7170
conf.getInteger(AmoroManagementConf.REFRESH_TABLES_THREAD_COUNT),
7271
conf.get(AmoroManagementConf.REFRESH_TABLES_INTERVAL).toMillis(),
7372
conf.getInteger(AmoroManagementConf.REFRESH_MAX_PENDING_PARTITIONS));
74-
if (conf.getBoolean(AmoroManagementConf.AUTO_CREATE_TAGS_ENABLED)) {
75-
this.tagsAutoCreatingExecutor =
76-
new TagsAutoCreatingExecutor(
77-
tableService,
78-
conf.getInteger(AmoroManagementConf.AUTO_CREATE_TAGS_THREAD_COUNT),
79-
conf.get(AmoroManagementConf.AUTO_CREATE_TAGS_INTERVAL).toMillis());
80-
}
8173
}
8274

8375
public TableRuntimeRefreshExecutor getTableRefreshingExecutor() {
@@ -99,8 +91,4 @@ public ProcessDataExpiringExecutor getProcessDataExpiringExecutor() {
9991
public HiveCommitSyncExecutor getHiveCommitSyncExecutor() {
10092
return hiveCommitSyncExecutor;
10193
}
102-
103-
public TagsAutoCreatingExecutor getTagsAutoCreatingExecutor() {
104-
return tagsAutoCreatingExecutor;
105-
}
10694
}

‎amoro-ams/src/main/java/org/apache/amoro/server/scheduler/inline/TagsAutoCreatingExecutor.java‎

Lines changed: 0 additions & 76 deletions
This file was deleted.

‎amoro-ams/src/test/java/org/apache/amoro/server/TestAmoroManagementConf.java‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public class TestAmoroManagementConf {
4545
private static final ConfigOption<Duration>[] TIME_RELATED_CONFIG_OPTIONS =
4646
new ConfigOption[] {
4747
AmoroManagementConf.REFRESH_EXTERNAL_CATALOGS_INTERVAL,
48-
AmoroManagementConf.AUTO_CREATE_TAGS_INTERVAL,
4948
AmoroManagementConf.REFRESH_TABLES_INTERVAL,
5049
AmoroManagementConf.BLOCKER_TIMEOUT,
5150
AmoroManagementConf.OPTIMIZER_HB_TIMEOUT,
@@ -57,7 +56,6 @@ public class TestAmoroManagementConf {
5756
private static final Map<String, String> DEFAULT_TIME_UNIT_IN_OLD_VERSIONS =
5857
ImmutableMap.<String, String>builder()
5958
.put(AmoroManagementConf.REFRESH_EXTERNAL_CATALOGS_INTERVAL.key(), "ms")
60-
.put(AmoroManagementConf.AUTO_CREATE_TAGS_INTERVAL.key(), "ms")
6159
.put(AmoroManagementConf.REFRESH_TABLES_INTERVAL.key(), "ms")
6260
.put(AmoroManagementConf.BLOCKER_TIMEOUT.key(), "ms")
6361
.put(AmoroManagementConf.OPTIMIZER_HB_TIMEOUT.key(), "ms")
@@ -228,7 +226,6 @@ private void assertStorageRelatedConfigs(
228226
.put("optimizer.task-ack-timeout", "30000")
229227
.put("optimizer.polling-timeout", "3000")
230228
.put("blocker.timeout", "60000")
231-
.put("auto-create-tags.interval", "60000")
232229
.put("terminal.session.timeout", "1800000")
233230
.build();
234231

@@ -240,7 +237,6 @@ private void assertStorageRelatedConfigs(
240237
.put("optimizer.task-ack-timeout", "60 s")
241238
.put("optimizer.polling-timeout", "6 s")
242239
.put("blocker.timeout", "2 min")
243-
.put("auto-create-tags.interval", "2 min")
244240
.put("terminal.session.timeout", "30 ms")
245241
.build();
246242

@@ -252,7 +248,6 @@ private void assertStorageRelatedConfigs(
252248
.put("optimizer.task-ack-timeout", "60 s")
253249
.put("optimizer.polling-timeout", "6 s")
254250
.put("blocker.timeout", "2 min")
255-
.put("auto-create-tags.interval", "2 min")
256251
.put("terminal.session.timeout", "30 min")
257252
.build();
258253

‎amoro-ams/src/test/java/org/apache/amoro/server/process/iceberg/TestIcebergProcessFactory.java‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.amoro.TableRuntime;
2828
import org.apache.amoro.config.DataExpirationConfig;
2929
import org.apache.amoro.config.TableConfiguration;
30+
import org.apache.amoro.config.TagConfiguration;
3031
import org.apache.amoro.process.LocalExecutionEngine;
3132
import org.apache.amoro.process.ProcessTriggerStrategy;
3233
import org.apache.amoro.process.RecoverProcessFailedException;
@@ -67,6 +68,8 @@ public void testTriggerActionWhenDue() {
6768
DanglingDeleteFilesCleaningProcess.class,
6869
0);
6970
assertTriggerWhenDue("expire-data", IcebergActions.EXPIRE_DATA, DataExpiringProcess.class, 0);
71+
assertTriggerWhenDue(
72+
"auto-create-tags", IcebergActions.AUTO_CREATE_TAGS, TagsAutoCreatingProcess.class, 0);
7073
}
7174

7275
@Test
@@ -89,6 +92,7 @@ public void testTriggerActionDisabled() {
8992
assertTriggerDisabled(
9093
"clean-dangling-delete-files", IcebergActions.CLEAN_DANGLING_DELETE, false, 0);
9194
assertTriggerDisabled("expire-data", IcebergActions.EXPIRE_DATA, false, 0);
95+
assertTriggerDisabled("auto-create-tags", IcebergActions.AUTO_CREATE_TAGS, false, 0);
9296
}
9397

9498
@Test
@@ -116,6 +120,12 @@ public void testRecoverDataExpiringProcess() {
116120
assertRecover("expire-data", IcebergActions.EXPIRE_DATA, DataExpiringProcess.class);
117121
}
118122

123+
@Test
124+
public void testRecoverAutoCreateTagProcess() {
125+
assertRecover(
126+
"auto-create-tags", IcebergActions.AUTO_CREATE_TAGS, TagsAutoCreatingProcess.class);
127+
}
128+
119129
@Test
120130
public void testRecoverUnsupportedActionThrows() {
121131
IcebergProcessFactory factory = openedFactory("expire-snapshots");
@@ -258,6 +268,10 @@ private TableRuntime createRuntime(String configKey, boolean enabled, long lastT
258268
tableConfiguration.setDeleteDanglingDeleteFilesEnabled(enabled);
259269
} else if ("expire-data".equals(configKey)) {
260270
tableConfiguration.setExpiringDataConfig(new DataExpirationConfig().setEnabled(enabled));
271+
} else if ("auto-create-tags".equals(configKey)) {
272+
TagConfiguration tagConfiguration = new TagConfiguration();
273+
tagConfiguration.setAutoCreateTag(enabled);
274+
tableConfiguration.setTagConfiguration(tagConfiguration);
261275
}
262276

263277
TableRuntimeCleanupState cleanupState = new TableRuntimeCleanupState();
@@ -274,7 +288,9 @@ private TableRuntime createRuntime(String configKey, boolean enabled, long lastT
274288
TableRuntime runtime = mock(TableRuntime.class);
275289
doReturn(tableConfiguration).when(runtime).getTableConfiguration();
276290
doReturn(cleanupState).when(runtime).getState(DefaultTableRuntime.CLEANUP_STATE_KEY);
277-
291+
if ("auto-create-tags".equals(configKey)) {
292+
doReturn(TableFormat.ICEBERG).when(runtime).getFormat();
293+
}
278294
return runtime;
279295
}
280296
}

0 commit comments

Comments
 (0)