|
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; |
| 10 | + |
| 11 | +import java.util.concurrent.TimeUnit; |
6 | 12 |
|
7 | 13 | final class ClientConfUtils { |
8 | 14 | private ClientConfUtils() { |
9 | 15 | } |
10 | 16 |
|
11 | | - static boolean patchConfStr(String confStr, StringSink sink) { |
12 | | - int pos = ConfStringParser.of(confStr, sink); |
| 17 | + |
| 18 | + static boolean patchConfStr(String confStr, StringSink sink, FlushConfig flushConfig) { |
| 19 | + flushConfig.reset(); |
| 20 | + |
| 21 | + sink.clear(); |
| 22 | + StringSink tmpSink = Misc.getThreadLocalSink(); |
| 23 | + int pos = ConfStringParser.of(confStr, tmpSink); |
13 | 24 | if (pos < 0) { |
14 | | - sink.clear(); |
15 | 25 | sink.put(confStr); |
16 | 26 | return false; |
17 | 27 | } |
18 | 28 |
|
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; |
| 29 | + boolean isHttpTransport = Chars.equals(tmpSink, "http") || Chars.equals(tmpSink, "https"); |
| 30 | + if (!isHttpTransport) { |
| 31 | + sink.put(confStr); |
| 32 | + // no patching for TCP transport |
| 33 | + return false; |
| 34 | + } |
| 35 | + sink.put(tmpSink).put("::"); |
24 | 36 |
|
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 |
| 37 | + boolean hasAtLeastOneParam = false; |
28 | 38 | while (ConfStringParser.hasNext(confStr, pos)) { |
29 | 39 | hasAtLeastOneParam = true; |
30 | | - pos = ConfStringParser.nextKey(confStr, pos, sink); |
| 40 | + pos = ConfStringParser.nextKey(confStr, pos, tmpSink); |
31 | 41 | if (pos < 0) { |
32 | | - parseError = true; |
33 | | - break; |
| 42 | + sink.clear(); |
| 43 | + sink.put(confStr); |
| 44 | + return true; |
34 | 45 | } |
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"); |
| 46 | + if (Chars.equals(tmpSink, "auto_flush_interval")) { |
| 47 | + pos = ConfStringParser.value(confStr, pos, tmpSink); |
| 48 | + if (pos < 0) { |
| 49 | + sink.clear(); |
| 50 | + sink.put(confStr); |
| 51 | + // invalid config, let the real client parser to fail |
| 52 | + return true; |
| 53 | + } |
| 54 | + if (Chars.equals(tmpSink, "off")) { |
| 55 | + throw new ConfigException("QuestDB Kafka connector cannot have auto_flush_interval disabled"); |
| 56 | + } |
| 57 | + try { |
| 58 | + flushConfig.autoFlushNanos = TimeUnit.MILLISECONDS.toNanos(Numbers.parseLong(tmpSink)); |
| 59 | + } catch (NumericException e) { |
| 60 | + throw new ConfigException("Invalid auto_flush_interval value [auto_flush_interval=" + tmpSink + ']'); |
| 61 | + } |
| 62 | + } else if (Chars.equals(tmpSink, "auto_flush_rows")) { |
| 63 | + pos = ConfStringParser.value(confStr, pos, tmpSink); |
| 64 | + if (pos < 0) { |
| 65 | + sink.clear(); |
| 66 | + sink.put(confStr); |
| 67 | + return true; |
| 68 | + } |
| 69 | + if (Chars.equals(tmpSink, "off")) { |
| 70 | + throw new ConfigException("QuestDB Kafka connector cannot have auto_flush_rows disabled"); |
| 71 | + } else { |
| 72 | + try { |
| 73 | + flushConfig.autoFlushRows = Numbers.parseInt(tmpSink); |
| 74 | + } catch (NumericException e) { |
| 75 | + throw new ConfigException("Invalid auto_flush_rows value [auto_flush_rows=" + tmpSink + ']'); |
| 76 | + } |
| 77 | + } |
| 78 | + } else if (Chars.equals(tmpSink, "auto_flush")) { |
| 79 | + pos = ConfStringParser.value(confStr, pos, tmpSink); |
| 80 | + if (pos < 0) { |
| 81 | + sink.clear(); |
| 82 | + sink.put(confStr); |
| 83 | + return true; |
| 84 | + } |
| 85 | + if (Chars.equals(tmpSink, "off")) { |
| 86 | + throw new ConfigException("QuestDB Kafka connector cannot have auto_flush disabled"); |
| 87 | + } else if (!Chars.equals(tmpSink, "on")) { |
| 88 | + throw new ConfigException("Unknown auto_flush value [auto_flush=" + tmpSink + ']'); |
| 89 | + } |
41 | 90 | } else { |
42 | | - pos = ConfStringParser.value(confStr, pos, sink); // skip other values |
43 | | - } |
44 | | - if (pos < 0) { |
45 | | - parseError = true; |
46 | | - break; |
| 91 | + // copy other params |
| 92 | + sink.put(tmpSink).put('='); |
| 93 | + pos = ConfStringParser.value(confStr, pos, tmpSink); |
| 94 | + if (pos < 0) { |
| 95 | + sink.clear(); |
| 96 | + sink.put(confStr); |
| 97 | + return true; |
| 98 | + } |
| 99 | + for (int i = 0; i < tmpSink.length(); i++) { |
| 100 | + char ch = tmpSink.charAt(i); |
| 101 | + sink.put(ch); |
| 102 | + // re-escape semicolon |
| 103 | + if (ch == ';') { |
| 104 | + sink.put(';'); |
| 105 | + } |
| 106 | + } |
| 107 | + sink.put(';'); |
47 | 108 | } |
48 | 109 | } |
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(';'); |
| 110 | + if (!hasAtLeastOneParam) { |
| 111 | + // this is invalid, let the real client parser to fail |
| 112 | + sink.clear(); |
| 113 | + sink.put(confStr); |
| 114 | + return true; |
62 | 115 | } |
| 116 | + sink.put("auto_flush=off;"); |
63 | 117 |
|
64 | | - return isHttpTransport; |
| 118 | + return true; |
65 | 119 | } |
66 | 120 | } |
0 commit comments