|
19 | 19 | import io.aiven.commons.kafka.config.fragment.BackoffPolicyFragment; |
20 | 20 | import io.aiven.commons.kafka.config.fragment.CommonConfigFragment; |
21 | 21 | import io.aiven.commons.kafka.config.fragment.FragmentDataAccess; |
22 | | -import org.apache.kafka.common.config.AbstractConfig; |
23 | | -import org.apache.kafka.common.config.ConfigDef; |
24 | | -import org.apache.kafka.common.config.ConfigException; |
25 | | -import org.apache.kafka.common.config.ConfigValue; |
26 | | - |
27 | 22 | import java.util.HashMap; |
28 | 23 | import java.util.List; |
29 | 24 | import java.util.Map; |
30 | 25 | import java.util.stream.Collectors; |
| 26 | +import org.apache.kafka.common.config.AbstractConfig; |
| 27 | +import org.apache.kafka.common.config.ConfigDef; |
| 28 | +import org.apache.kafka.common.config.ConfigException; |
| 29 | +import org.apache.kafka.common.config.ConfigValue; |
31 | 30 |
|
32 | | -/** |
33 | | - * The base configuration or all connectors. |
34 | | - */ |
| 31 | +/** The base configuration or all connectors. */ |
35 | 32 | public class CommonConfig extends AbstractConfig { |
36 | 33 |
|
37 | | - private final BackoffPolicyFragment backoffPolicyFragment; |
38 | | - private final CommonConfigFragment commonConfigFragment; |
39 | | - |
40 | | - /** |
41 | | - * Checks the configuration definition for errors. If any errors are found an |
42 | | - * exception is thrown. Due to the point at which this is called there are no |
43 | | - * {@link ConfigDef.ConfigKey#validator} errors to worry about. |
44 | | - * |
45 | | - * @param definition |
46 | | - * the ConfigDefinition to validate. |
47 | | - * @param props |
48 | | - * The map of parameter name to values to verify with. |
49 | | - */ |
50 | | - private void doVerification(final CommonConfigDef definition, final Map<String, String> props) { |
51 | | - Map<String, ConfigValue> configValueMap = definition.validateAll(props); |
52 | | - |
53 | | - // ensure that all the values are accounted for, not just those in the |
54 | | - // properties |
55 | | - this.values().forEach((k, v) -> { |
56 | | - if (configValueMap.get(k) != null) { |
57 | | - configValueMap.get(k).value(v); |
58 | | - } |
59 | | - }); |
60 | | - |
61 | | - // process the detailed validation. |
62 | | - definition.multiValidate(configValueMap); |
63 | | - |
64 | | - // if there are any reported errors produce a detailed configuration exception. |
65 | | - final List<ConfigValue> errorConfigs = configValueMap.values().stream() |
66 | | - .filter(configValue -> !configValue.errorMessages().isEmpty()).toList(); |
67 | | - if (!errorConfigs.isEmpty()) { |
68 | | - final String msg = errorConfigs.stream().flatMap(configValue -> configValue.errorMessages().stream()) |
69 | | - .collect(Collectors.joining("\n")); |
70 | | - throw new ConfigException("There are errors in the configuration:\n" + msg); |
71 | | - } |
72 | | - } |
73 | | - |
74 | | - /** |
75 | | - * Constructor. |
76 | | - * |
77 | | - * @param definition |
78 | | - * CommonConfigDef based definition to use. |
79 | | - * @param originals |
80 | | - * the original property name to value map. |
81 | | - */ |
82 | | - public CommonConfig(final CommonConfigDef definition, final Map<String, String> originals) { |
83 | | - super(definition, originals); |
84 | | - doVerification(definition, originals); |
85 | | - final FragmentDataAccess dataAccess = FragmentDataAccess.from(this); |
86 | | - commonConfigFragment = new CommonConfigFragment(dataAccess); |
87 | | - backoffPolicyFragment = new BackoffPolicyFragment(dataAccess); |
88 | | - } |
89 | | - |
90 | | - /** |
91 | | - * Avoid Finalizer attack |
92 | | - */ |
93 | | - @Override |
94 | | - @SuppressWarnings("PMD.EmptyFinalizer") |
95 | | - protected final void finalize() { |
96 | | - // Do nothing |
97 | | - } |
98 | | - |
99 | | - @Override |
100 | | - final protected Map<String, Object> postProcessParsedConfig(Map<String, Object> parsedValues) { |
101 | | - ChangeTrackingMap result = new ChangeTrackingMap(parsedValues); |
102 | | - fragmentPostProcess(result); |
103 | | - return result.override; |
104 | | - } |
105 | | - |
106 | | - /** |
107 | | - * Allows implementations to modify the values of the map from the fragments. |
108 | | - * Default implementation does nothing. |
109 | | - * |
110 | | - * @param map |
111 | | - * the map to make changes in. |
112 | | - */ |
113 | | - protected void fragmentPostProcess(ChangeTrackingMap map) { |
114 | | - // does nothing. |
115 | | - } |
116 | | - |
117 | | - /** |
118 | | - * Gets the Kafka retry backoff time in MS. |
119 | | - * |
120 | | - * @return The Kafka retry backoff time in MS. |
121 | | - */ |
122 | | - public Long getKafkaRetryBackoffMs() { |
123 | | - return backoffPolicyFragment.getKafkaRetryBackoffMs(); |
124 | | - } |
125 | | - |
126 | | - /** |
127 | | - * |
128 | | - * Get the maximum number of tasks that should be run by this connector |
129 | | - * configuration Max Tasks is set within the Kafka Connect framework and so is |
130 | | - * retrieved slightly differently in ConnectorConfig.java |
131 | | - * |
132 | | - * @return The maximum number of tasks that should be run by this connector |
133 | | - * configuration |
134 | | - */ |
135 | | - public int getMaxTasks() { |
136 | | - return commonConfigFragment.getMaxTasks(); |
137 | | - } |
138 | | - /** |
139 | | - * Get the task id for this configuration |
140 | | - * |
141 | | - * @return The task id for this configuration |
142 | | - */ |
143 | | - public int getTaskId() { |
144 | | - return commonConfigFragment.getTaskId(); |
145 | | - } |
146 | | - |
147 | | - /** |
148 | | - * A map of values that allows overrides. |
149 | | - */ |
150 | | - public static class ChangeTrackingMap { |
151 | | - private final Map<String, Object> baseMap; |
152 | | - private final Map<String, Object> override; |
153 | | - |
154 | | - /** |
155 | | - * Constructor. |
156 | | - * |
157 | | - * @param baseMap |
158 | | - * the original map. |
159 | | - */ |
160 | | - public ChangeTrackingMap(Map<String, Object> baseMap) { |
161 | | - this.baseMap = baseMap; |
162 | | - this.override = new HashMap<>(); |
163 | | - } |
164 | | - |
165 | | - /** |
166 | | - * Sets the override for a key. Passing {@code null} removes any override, any |
167 | | - * other values sets the override. |
168 | | - * |
169 | | - * @param key |
170 | | - * the key to override. |
171 | | - * @param value |
172 | | - * the value to set the key to. |
173 | | - */ |
174 | | - public void override(String key, Object value) { |
175 | | - if (value == null) { |
176 | | - override.remove(key); |
177 | | - } else { |
178 | | - override.put(key, value); |
179 | | - } |
180 | | - } |
181 | | - |
182 | | - /** |
183 | | - * Gets the current value of the key. This is the last override or the current |
184 | | - * value if no override is present. |
185 | | - * |
186 | | - * @param key |
187 | | - * the key to get the value for. |
188 | | - * @return the current value. |
189 | | - */ |
190 | | - public Object get(String key) { |
191 | | - Object result = override.get(key); |
192 | | - return result == null ? baseMap.get(key) : result; |
193 | | - } |
194 | | - |
195 | | - } |
| 34 | + private final BackoffPolicyFragment backoffPolicyFragment; |
| 35 | + private final CommonConfigFragment commonConfigFragment; |
| 36 | + |
| 37 | + /** |
| 38 | + * Checks the configuration definition for errors. If any errors are found an exception is thrown. |
| 39 | + * Due to the point at which this is called there are no {@link ConfigDef.ConfigKey#validator} |
| 40 | + * errors to worry about. |
| 41 | + * |
| 42 | + * @param definition the ConfigDefinition to validate. |
| 43 | + * @param props The map of parameter name to values to verify with. |
| 44 | + */ |
| 45 | + private void doVerification(final CommonConfigDef definition, final Map<String, String> props) { |
| 46 | + Map<String, ConfigValue> configValueMap = definition.validateAll(props); |
| 47 | + |
| 48 | + // ensure that all the values are accounted for, not just those in the |
| 49 | + // properties |
| 50 | + this.values() |
| 51 | + .forEach( |
| 52 | + (k, v) -> { |
| 53 | + if (configValueMap.get(k) != null) { |
| 54 | + configValueMap.get(k).value(v); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + // process the detailed validation. |
| 59 | + definition.multiValidate(configValueMap); |
| 60 | + |
| 61 | + // if there are any reported errors produce a detailed configuration exception. |
| 62 | + final List<ConfigValue> errorConfigs = |
| 63 | + configValueMap.values().stream() |
| 64 | + .filter(configValue -> !configValue.errorMessages().isEmpty()) |
| 65 | + .toList(); |
| 66 | + if (!errorConfigs.isEmpty()) { |
| 67 | + final String msg = |
| 68 | + errorConfigs.stream() |
| 69 | + .flatMap(configValue -> configValue.errorMessages().stream()) |
| 70 | + .collect(Collectors.joining("\n")); |
| 71 | + throw new ConfigException("There are errors in the configuration:\n" + msg); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Constructor. |
| 77 | + * |
| 78 | + * @param definition CommonConfigDef based definition to use. |
| 79 | + * @param originals the original property name to value map. |
| 80 | + */ |
| 81 | + public CommonConfig(final CommonConfigDef definition, final Map<String, String> originals) { |
| 82 | + super(definition, originals); |
| 83 | + doVerification(definition, originals); |
| 84 | + final FragmentDataAccess dataAccess = FragmentDataAccess.from(this); |
| 85 | + commonConfigFragment = new CommonConfigFragment(dataAccess); |
| 86 | + backoffPolicyFragment = new BackoffPolicyFragment(dataAccess); |
| 87 | + } |
| 88 | + |
| 89 | + /** Avoid Finalizer attack */ |
| 90 | + @Override |
| 91 | + @SuppressWarnings("PMD.EmptyFinalizer") |
| 92 | + protected final void finalize() { |
| 93 | + // Do nothing |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + protected final Map<String, Object> postProcessParsedConfig(Map<String, Object> parsedValues) { |
| 98 | + ChangeTrackingMap result = new ChangeTrackingMap(parsedValues); |
| 99 | + fragmentPostProcess(result); |
| 100 | + return result.override; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Allows implementations to modify the values of the map from the fragments. Default |
| 105 | + * implementation does nothing. |
| 106 | + * |
| 107 | + * @param map the map to make changes in. |
| 108 | + */ |
| 109 | + protected void fragmentPostProcess(ChangeTrackingMap map) { |
| 110 | + // does nothing. |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Gets the Kafka retry backoff time in MS. |
| 115 | + * |
| 116 | + * @return The Kafka retry backoff time in MS. |
| 117 | + */ |
| 118 | + public Long getKafkaRetryBackoffMs() { |
| 119 | + return backoffPolicyFragment.getKafkaRetryBackoffMs(); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Get the maximum number of tasks that should be run by this connector configuration Max Tasks is |
| 124 | + * set within the Kafka Connect framework and so is retrieved slightly differently in |
| 125 | + * ConnectorConfig.java |
| 126 | + * |
| 127 | + * @return The maximum number of tasks that should be run by this connector configuration |
| 128 | + */ |
| 129 | + public int getMaxTasks() { |
| 130 | + return commonConfigFragment.getMaxTasks(); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Get the task id for this configuration |
| 135 | + * |
| 136 | + * @return The task id for this configuration |
| 137 | + */ |
| 138 | + public int getTaskId() { |
| 139 | + return commonConfigFragment.getTaskId(); |
| 140 | + } |
| 141 | + |
| 142 | + /** A map of values that allows overrides. */ |
| 143 | + public static class ChangeTrackingMap { |
| 144 | + private final Map<String, Object> baseMap; |
| 145 | + private final Map<String, Object> override; |
| 146 | + |
| 147 | + /** |
| 148 | + * Constructor. |
| 149 | + * |
| 150 | + * @param baseMap the original map. |
| 151 | + */ |
| 152 | + public ChangeTrackingMap(Map<String, Object> baseMap) { |
| 153 | + this.baseMap = baseMap; |
| 154 | + this.override = new HashMap<>(); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Sets the override for a key. Passing {@code null} removes any override, any other values sets |
| 159 | + * the override. |
| 160 | + * |
| 161 | + * @param key the key to override. |
| 162 | + * @param value the value to set the key to. |
| 163 | + */ |
| 164 | + public void override(String key, Object value) { |
| 165 | + if (value == null) { |
| 166 | + override.remove(key); |
| 167 | + } else { |
| 168 | + override.put(key, value); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Gets the current value of the key. This is the last override or the current value if no |
| 174 | + * override is present. |
| 175 | + * |
| 176 | + * @param key the key to get the value for. |
| 177 | + * @return the current value. |
| 178 | + */ |
| 179 | + public Object get(String key) { |
| 180 | + Object result = override.get(key); |
| 181 | + return result == null ? baseMap.get(key) : result; |
| 182 | + } |
| 183 | + } |
196 | 184 | } |
0 commit comments