|
| 1 | +/* |
| 2 | + * Copyright 2023 Exactpro (Exactpro Systems Limited) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.exactpro.th2.mstore; |
| 17 | + |
| 18 | +import com.exactpro.th2.common.event.Event; |
| 19 | +import com.exactpro.th2.common.event.Event.Status; |
| 20 | +import com.exactpro.th2.common.event.IBodyData; |
| 21 | +import com.exactpro.th2.common.grpc.EventBatch; |
| 22 | +import com.exactpro.th2.common.grpc.EventID; |
| 23 | +import com.exactpro.th2.common.schema.message.MessageRouter; |
| 24 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 25 | +import org.jetbrains.annotations.NotNull; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.time.Instant; |
| 31 | +import java.util.Collection; |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.concurrent.ScheduledExecutorService; |
| 35 | +import java.util.concurrent.ScheduledFuture; |
| 36 | +import java.util.concurrent.TimeUnit; |
| 37 | +import java.util.concurrent.locks.Lock; |
| 38 | +import java.util.concurrent.locks.ReentrantLock; |
| 39 | + |
| 40 | +import static java.util.Objects.requireNonNull; |
| 41 | + |
| 42 | +@SuppressWarnings("unused") |
| 43 | +public class ErrorCollector implements AutoCloseable { |
| 44 | + private static final Logger LOGGER = LoggerFactory.getLogger(ErrorCollector.class); |
| 45 | + private final ScheduledFuture<?> drainFuture; |
| 46 | + private final MessageRouter<EventBatch> eventRouter; |
| 47 | + private final EventID rootEvent; |
| 48 | + private final Lock lock = new ReentrantLock(); |
| 49 | + private Map<String, ErrorMetadata> errors = new HashMap<>(); |
| 50 | + |
| 51 | + public ErrorCollector(@NotNull ScheduledExecutorService executor, |
| 52 | + @NotNull MessageRouter<EventBatch> eventRouter, |
| 53 | + @NotNull EventID rootEvent, |
| 54 | + long period, |
| 55 | + @NotNull TimeUnit unit) { |
| 56 | + this.eventRouter = requireNonNull(eventRouter, "Event router can't be null"); |
| 57 | + this.rootEvent = requireNonNull(rootEvent, "Root event can't be null"); |
| 58 | + requireNonNull(unit, "Unit can't be null"); |
| 59 | + this.drainFuture = requireNonNull(executor, "Executor can't be null") |
| 60 | + .scheduleAtFixedRate(this::drain, period, period, unit); |
| 61 | + } |
| 62 | + |
| 63 | + public ErrorCollector(@NotNull ScheduledExecutorService executor, |
| 64 | + @NotNull MessageRouter<EventBatch> eventRouter, |
| 65 | + @NotNull EventID rootEvent) { |
| 66 | + this(executor, eventRouter, rootEvent, 1, TimeUnit.MINUTES); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Log error and call the {@link #collect(String)}} method |
| 71 | + * @param error is used as key identifier. Avoid put a lot of unique values |
| 72 | + */ |
| 73 | + public void collect(Logger logger, String error, Throwable cause) { |
| 74 | + logger.error(error, cause); |
| 75 | + collect(error); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param error is used as key identifier. Avoid put a lot of unique values |
| 80 | + */ |
| 81 | + public void collect(String error) { |
| 82 | + lock.lock(); |
| 83 | + try { |
| 84 | + errors.compute(error, (key, metadata) -> { |
| 85 | + if (metadata == null) { |
| 86 | + return new ErrorMetadata(); |
| 87 | + } |
| 88 | + metadata.inc(); |
| 89 | + return metadata; |
| 90 | + }); |
| 91 | + } finally { |
| 92 | + lock.unlock(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void close() throws Exception { |
| 98 | + drainFuture.cancel(true); |
| 99 | + drain(); |
| 100 | + } |
| 101 | + |
| 102 | + private void drain() { |
| 103 | + try { |
| 104 | + Map<String, ErrorMetadata> map = clear(); |
| 105 | + if (map.isEmpty()) { return; } |
| 106 | + |
| 107 | + eventRouter.sendAll(Event.start() |
| 108 | + .name("mstore internal problem(s): " + calculateTotalQty(map.values())) |
| 109 | + .type("InternalError") |
| 110 | + .status(Status.FAILED) |
| 111 | + .bodyData(new BodyData(map)) |
| 112 | + .toBatchProto(rootEvent)); |
| 113 | + |
| 114 | + } catch (IOException | RuntimeException e) { |
| 115 | + LOGGER.error("Drain events task failure", e); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private Map<String, ErrorMetadata> clear() { |
| 120 | + lock.lock(); |
| 121 | + try { |
| 122 | + Map<String, ErrorMetadata> result = errors; |
| 123 | + errors = new HashMap<>(); |
| 124 | + return result; |
| 125 | + } finally { |
| 126 | + lock.unlock(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private static int calculateTotalQty(Collection<ErrorMetadata> errors) { |
| 131 | + return errors.stream() |
| 132 | + .map(ErrorMetadata::getQuantity) |
| 133 | + .reduce(0, Integer::sum); |
| 134 | + } |
| 135 | + |
| 136 | + private static class BodyData implements IBodyData { |
| 137 | + private final Map<String, ErrorMetadata> errors; |
| 138 | + @JsonCreator |
| 139 | + private BodyData(Map<String, ErrorMetadata> errors) { |
| 140 | + this.errors = errors; |
| 141 | + } |
| 142 | + public Map<String, ErrorMetadata> getErrors() { |
| 143 | + return errors; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private static class ErrorMetadata { |
| 148 | + private final Instant firstDate = Instant.now(); |
| 149 | + private Instant lastDate; |
| 150 | + private int quantity = 1; |
| 151 | + |
| 152 | + public void inc() { |
| 153 | + quantity += 1; |
| 154 | + lastDate = Instant.now(); |
| 155 | + } |
| 156 | + |
| 157 | + public Instant getFirstDate() { |
| 158 | + return firstDate; |
| 159 | + } |
| 160 | + |
| 161 | + public Instant getLastDate() { |
| 162 | + return lastDate; |
| 163 | + } |
| 164 | + |
| 165 | + public void setLastDate(Instant lastDate) { |
| 166 | + this.lastDate = lastDate; |
| 167 | + } |
| 168 | + |
| 169 | + public int getQuantity() { |
| 170 | + return quantity; |
| 171 | + } |
| 172 | + |
| 173 | + public void setQuantity(int quantity) { |
| 174 | + this.quantity = quantity; |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments