|
2 | 2 |
|
3 | 3 | import io.questdb.client.impl.ConfStringParser;
|
4 | 4 | import io.questdb.std.Chars;
|
| 5 | +import io.questdb.std.Misc; |
| 6 | +import io.questdb.std.Numbers; |
| 7 | +import io.questdb.std.NumericException; |
5 | 8 | import io.questdb.std.str.StringSink;
|
| 9 | +import org.apache.kafka.common.config.ConfigException; |
6 | 10 |
|
7 | 11 | final class ClientConfUtils {
|
8 | 12 | private ClientConfUtils() {
|
9 | 13 | }
|
10 | 14 |
|
11 |
| - static boolean patchConfStr(String confStr, StringSink sink) { |
12 |
| - int pos = ConfStringParser.of(confStr, sink); |
| 15 | + |
| 16 | + static boolean patchConfStr(String confStr, StringSink sink, FlushConfig flushConfig) { |
| 17 | + flushConfig.reset(); |
| 18 | + |
| 19 | + sink.clear(); |
| 20 | + StringSink tmpSink = Misc.getThreadLocalSink(); |
| 21 | + int pos = ConfStringParser.of(confStr, tmpSink); |
13 | 22 | if (pos < 0) {
|
14 |
| - sink.clear(); |
15 | 23 | sink.put(confStr);
|
16 | 24 | return false;
|
17 | 25 | }
|
18 | 26 |
|
19 |
| - boolean isHttpTransport = Chars.equals(sink, "http") || Chars.equals(sink, "https"); |
20 |
| - boolean intervalFlushSetExplicitly = false; |
21 |
| - boolean flushesDisabled = false; |
22 |
| - boolean parseError = false; |
23 |
| - boolean hasAtLeastOneParam = false; |
| 27 | + boolean isHttpTransport = Chars.equals(tmpSink, "http") || Chars.equals(tmpSink, "https"); |
| 28 | + if (!isHttpTransport) { |
| 29 | + sink.put(confStr); |
| 30 | + // no patching for TCP transport |
| 31 | + return false; |
| 32 | + } |
| 33 | + sink.put(tmpSink).put("::"); |
24 | 34 |
|
25 |
| - // disable interval based flushes |
26 |
| - // unless they are explicitly set or auto_flush is entirely off |
27 |
| - // why? the connector has its own mechanism to flush data in a timely manner |
| 35 | + boolean hasAtLeastOneParam = false; |
28 | 36 | while (ConfStringParser.hasNext(confStr, pos)) {
|
29 | 37 | hasAtLeastOneParam = true;
|
30 |
| - pos = ConfStringParser.nextKey(confStr, pos, sink); |
| 38 | + pos = ConfStringParser.nextKey(confStr, pos, tmpSink); |
31 | 39 | if (pos < 0) {
|
32 |
| - parseError = true; |
33 |
| - break; |
| 40 | + sink.clear(); |
| 41 | + sink.put(confStr); |
| 42 | + return true; |
34 | 43 | }
|
35 |
| - if (Chars.equals(sink, "auto_flush_interval")) { |
36 |
| - intervalFlushSetExplicitly = true; |
37 |
| - pos = ConfStringParser.value(confStr, pos, sink); |
38 |
| - } else if (Chars.equals(sink, "auto_flush")) { |
39 |
| - pos = ConfStringParser.value(confStr, pos, sink); |
40 |
| - flushesDisabled = Chars.equals(sink, "off"); |
| 44 | + if (Chars.equals(tmpSink, "auto_flush_interval")) { |
| 45 | + pos = ConfStringParser.value(confStr, pos, tmpSink); |
| 46 | + if (pos < 0) { |
| 47 | + sink.clear(); |
| 48 | + sink.put(confStr); |
| 49 | + // invalid config, let the real client parser to fail |
| 50 | + return true; |
| 51 | + } |
| 52 | + if (Chars.equals(tmpSink, "off")) { |
| 53 | + throw new ConfigException("QuestDB Kafka connector cannot have auto_flush_interval disabled"); |
| 54 | + } |
| 55 | + try { |
| 56 | + flushConfig.autoFlushNanos = Numbers.parseLong(tmpSink); |
| 57 | + } catch (NumericException e) { |
| 58 | + throw new ConfigException("Invalid auto_flush_interval value [auto_flush_interval=" + tmpSink + ']'); |
| 59 | + } |
| 60 | + } else if (Chars.equals(tmpSink, "auto_flush_rows")) { |
| 61 | + pos = ConfStringParser.value(confStr, pos, tmpSink); |
| 62 | + if (pos < 0) { |
| 63 | + sink.clear(); |
| 64 | + sink.put(confStr); |
| 65 | + return true; |
| 66 | + } |
| 67 | + if (Chars.equals(tmpSink, "off")) { |
| 68 | + throw new ConfigException("QuestDB Kafka connector cannot have auto_flush_rows disabled"); |
| 69 | + } else { |
| 70 | + try { |
| 71 | + flushConfig.autoFlushRows = Numbers.parseInt(tmpSink); |
| 72 | + } catch (NumericException e) { |
| 73 | + throw new ConfigException("Invalid auto_flush_rows value [auto_flush_rows=" + tmpSink + ']'); |
| 74 | + } |
| 75 | + } |
| 76 | + } else if (Chars.equals(tmpSink, "auto_flush")) { |
| 77 | + pos = ConfStringParser.value(confStr, pos, tmpSink); |
| 78 | + if (pos < 0) { |
| 79 | + sink.clear(); |
| 80 | + sink.put(confStr); |
| 81 | + return true; |
| 82 | + } |
| 83 | + if (Chars.equals(tmpSink, "off")) { |
| 84 | + throw new ConfigException("QuestDB Kafka connector cannot have auto_flush disabled"); |
| 85 | + } else if (!Chars.equals(tmpSink, "on")) { |
| 86 | + throw new ConfigException("Unknown auto_flush value [auto_flush=" + tmpSink + ']'); |
| 87 | + } |
41 | 88 | } else {
|
42 |
| - pos = ConfStringParser.value(confStr, pos, sink); // skip other values |
43 |
| - } |
44 |
| - if (pos < 0) { |
45 |
| - parseError = true; |
46 |
| - break; |
| 89 | + // copy other params |
| 90 | + sink.put(tmpSink).put('='); |
| 91 | + pos = ConfStringParser.value(confStr, pos, tmpSink); |
| 92 | + if (pos < 0) { |
| 93 | + sink.clear(); |
| 94 | + sink.put(confStr); |
| 95 | + return true; |
| 96 | + } |
| 97 | + for (int i = 0; i < tmpSink.length(); i++) { |
| 98 | + char ch = tmpSink.charAt(i); |
| 99 | + sink.put(ch); |
| 100 | + // re-escape semicolon |
| 101 | + if (ch == ';') { |
| 102 | + sink.put(';'); |
| 103 | + } |
| 104 | + } |
| 105 | + sink.put(';'); |
47 | 106 | }
|
48 | 107 | }
|
49 |
| - sink.clear(); |
50 |
| - sink.put(confStr); |
51 |
| - if (!parseError // we don't want to mess with the config if there was a parse error |
52 |
| - && isHttpTransport // we only want to patch http transport |
53 |
| - && !flushesDisabled // if auto-flush is disabled we don't need to do anything |
54 |
| - && !intervalFlushSetExplicitly // if auto_flush_interval is set explicitly we don't want to override it |
55 |
| - && hasAtLeastOneParam // no parameter is also an error since at least address should be set. we let client throw exception in this case |
56 |
| - ) { |
57 |
| - // if everything is ok, we set auto_flush_interval to max value |
58 |
| - // this will effectively disable interval based flushes |
59 |
| - // and the connector will flush data only when it is told to do so by Connector |
60 |
| - // or if a row count limit is reached |
61 |
| - sink.put("auto_flush_interval=").put(Integer.MAX_VALUE).put(';'); |
| 108 | + if (!hasAtLeastOneParam) { |
| 109 | + // this is invalid, let the real client parser to fail |
| 110 | + sink.clear(); |
| 111 | + sink.put(confStr); |
| 112 | + return true; |
62 | 113 | }
|
| 114 | + sink.put("auto_flush=off;"); |
63 | 115 |
|
64 |
| - return isHttpTransport; |
| 116 | + return true; |
65 | 117 | }
|
66 | 118 | }
|
0 commit comments