|
| 1 | +/* |
| 2 | + * Teragrep Azure Eventhub Reader |
| 3 | + * Copyright (C) 2023 Suomen Kanuuna Oy |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Affero General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Affero General Public License |
| 16 | + * along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>. |
| 17 | + * |
| 18 | + * |
| 19 | + * Additional permission under GNU Affero General Public License version 3 |
| 20 | + * section 7 |
| 21 | + * |
| 22 | + * If you modify this Program, or any covered work, by linking or combining it |
| 23 | + * with other code, such other code is not for that reason alone subject to any |
| 24 | + * of the requirements of the GNU Affero GPL version 3 as long as this Program |
| 25 | + * is the same Program as licensed from Suomen Kanuuna Oy without any additional |
| 26 | + * modifications. |
| 27 | + * |
| 28 | + * Supplemented terms under GNU Affero General Public License version 3 |
| 29 | + * section 7 |
| 30 | + * |
| 31 | + * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified |
| 32 | + * versions must be marked as "Modified version of" The Program. |
| 33 | + * |
| 34 | + * Names of the licensors and authors may not be used for publicity purposes. |
| 35 | + * |
| 36 | + * No rights are granted for use of trade names, trademarks, or service marks |
| 37 | + * which are in The Program if any. |
| 38 | + * |
| 39 | + * Licensee must indemnify licensors and authors for any liability that these |
| 40 | + * contractual assumptions impose on licensors and authors. |
| 41 | + * |
| 42 | + * To the extent this program is licensed as part of the Commercial versions of |
| 43 | + * Teragrep, the applicable Commercial License may apply to this file if you as |
| 44 | + * a licensee so wish it. |
| 45 | + */ |
| 46 | +package com.teragrep.aer_01; |
| 47 | + |
| 48 | +import com.codahale.metrics.Counter; |
| 49 | +import com.codahale.metrics.MetricRegistry; |
| 50 | +import com.codahale.metrics.Timer; |
| 51 | +import com.teragrep.aer_01.config.RelpConfig; |
| 52 | +import com.teragrep.rlp_01.RelpBatch; |
| 53 | +import com.teragrep.rlp_01.RelpConnection; |
| 54 | +import org.slf4j.Logger; |
| 55 | +import org.slf4j.LoggerFactory; |
| 56 | + |
| 57 | +import java.io.IOException; |
| 58 | +import java.nio.channels.UnresolvedAddressException; |
| 59 | +import java.util.concurrent.TimeoutException; |
| 60 | + |
| 61 | +import static com.codahale.metrics.MetricRegistry.name; |
| 62 | + |
| 63 | +// TODO unify, this is a copy from cfe_35 which is a copy from rlo_10 with FIXES |
| 64 | +final class DefaultOutput implements Output { |
| 65 | + private static final Logger LOGGER = LoggerFactory.getLogger(DefaultOutput.class); |
| 66 | + |
| 67 | + private final RelpConnection relpConnection; |
| 68 | + private final String relpAddress; |
| 69 | + private final int relpPort; |
| 70 | + private final int reconnectInterval; |
| 71 | + |
| 72 | + // metrics |
| 73 | + private final Counter records; |
| 74 | + private final Counter bytes; |
| 75 | + private final Counter resends; |
| 76 | + private final Counter connects; |
| 77 | + private final Counter retriedConnects; |
| 78 | + private final Timer sendLatency; |
| 79 | + private final Timer connectLatency; |
| 80 | + |
| 81 | + |
| 82 | + DefaultOutput( |
| 83 | + String name, |
| 84 | + RelpConfig relpConfig, |
| 85 | + MetricRegistry metricRegistry) { |
| 86 | + this.relpAddress = relpConfig.destinationAddress; |
| 87 | + this.relpPort = relpConfig.destinationPort; |
| 88 | + this.reconnectInterval = relpConfig.reconnectInterval; |
| 89 | + |
| 90 | + this.relpConnection = new RelpConnection(); |
| 91 | + this.relpConnection.setConnectionTimeout(relpConfig.connectionTimeout); |
| 92 | + this.relpConnection.setReadTimeout(relpConfig.readTimeout); |
| 93 | + this.relpConnection.setWriteTimeout(relpConfig.writeTimeout); |
| 94 | + |
| 95 | + this.records = metricRegistry.counter(name(DefaultOutput.class, "<[" + name + "]>", "records")); |
| 96 | + this.bytes = metricRegistry.counter(name(DefaultOutput.class, "<[" + name + "]>", "bytes")); |
| 97 | + this.resends = metricRegistry.counter(name(DefaultOutput.class, "<[" + name + "]>", "resends")); |
| 98 | + this.connects = metricRegistry.counter(name(DefaultOutput.class, "<[" + name + "]>", "connects")); |
| 99 | + this.retriedConnects = metricRegistry.counter(name(DefaultOutput.class, "<[" + name + "]>", "retriedConnects")); |
| 100 | + this.sendLatency = metricRegistry.timer(name(DefaultOutput.class, "<[" + name + "]>", "sendLatency")); |
| 101 | + this.connectLatency = metricRegistry.timer(name(DefaultOutput.class, "<[" + name + "]>", "connectLatency")); |
| 102 | + |
| 103 | + connect(); |
| 104 | + } |
| 105 | + |
| 106 | + private void connect() { |
| 107 | + boolean connected = false; |
| 108 | + while (!connected) { |
| 109 | + try (final Timer.Context context = connectLatency.time()) { |
| 110 | + connected = this.relpConnection.connect(relpAddress, relpPort); |
| 111 | + connects.inc(); |
| 112 | + } catch (IOException | TimeoutException e) { |
| 113 | + LOGGER.error("Exception while connecting to <[{}]>:<[{}]>", relpAddress, relpPort, e); |
| 114 | + } catch (UnresolvedAddressException e) { |
| 115 | + LOGGER.error("Can't resolve address of target <[{}]>", relpAddress, e); |
| 116 | + } |
| 117 | + |
| 118 | + if (!connected) { |
| 119 | + try { |
| 120 | + Thread.sleep(reconnectInterval); |
| 121 | + retriedConnects.inc(); |
| 122 | + } catch (InterruptedException e) { |
| 123 | + LOGGER.warn("Sleep interrupted while waiting for reconnectInterval <[{}]> on <[{}]>:<[{}]>", reconnectInterval, relpAddress, relpPort, e); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + @Override |
| 131 | + public void accept(byte[] syslogMessage) { |
| 132 | + try (final Timer.Context context = sendLatency.time()) { |
| 133 | + RelpBatch batch = new RelpBatch(); |
| 134 | + batch.insert(syslogMessage); |
| 135 | + |
| 136 | + boolean allSent = false; |
| 137 | + while (!allSent) { |
| 138 | + try { |
| 139 | + this.relpConnection.commit(batch); |
| 140 | + |
| 141 | + // metrics |
| 142 | + // NOTICE these if batch size changes |
| 143 | + records.inc(1); |
| 144 | + bytes.inc(syslogMessage.length); |
| 145 | + |
| 146 | + } catch (IllegalStateException | IOException | TimeoutException e) { |
| 147 | + LOGGER.error("Exception while committing a batch to <[{}]>:<[{}]>", relpAddress, relpPort, e); |
| 148 | + } |
| 149 | + // Check if everything has been sent, retry and reconnect if not. |
| 150 | + if (!batch.verifyTransactionAll()) { |
| 151 | + batch.retryAllFailed(); |
| 152 | + |
| 153 | + // metrics |
| 154 | + // NOTICE this if batch size changes |
| 155 | + resends.inc(1); |
| 156 | + relpConnection.tearDown(); |
| 157 | + try { |
| 158 | + Thread.sleep(reconnectInterval); |
| 159 | + } catch(InterruptedException e) { |
| 160 | + throw new RuntimeException(e); |
| 161 | + } |
| 162 | + connect(); |
| 163 | + } else { |
| 164 | + allSent = true; |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public String toString() { |
| 172 | + return "DefaultOutput{" + |
| 173 | + "relpAddress='" + relpAddress + '\'' + |
| 174 | + ", relpPort=" + relpPort + |
| 175 | + '}'; |
| 176 | + } |
| 177 | + |
| 178 | + public void close() { |
| 179 | + try { |
| 180 | + relpConnection.disconnect(); |
| 181 | + } |
| 182 | + catch (IOException | TimeoutException e) { |
| 183 | + LOGGER.warn("Exception while disconnecting from <[{}]>:<[{}]>", relpAddress, relpPort, e); |
| 184 | + } |
| 185 | + finally { |
| 186 | + relpConnection.tearDown(); |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments