Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactoring] Split AbstractHistoryDataStorage class #1926

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 35,7 @@
import org.apache.hertzbeat.common.entity.dto.Value;
import org.apache.hertzbeat.common.entity.dto.ValueRow;
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.apache.hertzbeat.warehouse.store.history.AbstractHistoryDataStorage;
import org.apache.hertzbeat.warehouse.store.history.HistoryDataReader;
import org.apache.hertzbeat.warehouse.store.history.jpa.JpaDatabaseDataStorage;
import org.apache.hertzbeat.warehouse.store.realtime.AbstractRealTimeDataStorage;
import org.apache.hertzbeat.warehouse.store.realtime.memory.MemoryDataStorage;
Expand All @@ -58,20 58,20 @@ public class MetricsDataController {

private final List<AbstractRealTimeDataStorage> realTimeDataStorages;

private final List<AbstractHistoryDataStorage> historyDataStorages;
private final List<HistoryDataReader> historyDataReaders;

public MetricsDataController(List<AbstractRealTimeDataStorage> realTimeDataStorages,
List<AbstractHistoryDataStorage> historyDataStorages) {
List<HistoryDataReader> historyDataReaders) {
this.realTimeDataStorages = realTimeDataStorages;
this.historyDataStorages = historyDataStorages;
this.historyDataReaders = historyDataReaders;
}

@GetMapping("/api/warehouse/storage/status")
@Operation(summary = "Query Warehouse Storage Server Status", description = "Query the availability status of the storage service under the warehouse")
public ResponseEntity<Message<Void>> getWarehouseStorageServerStatus() {
boolean available = false;
if (historyDataStorages != null) {
available = historyDataStorages.stream().anyMatch(AbstractHistoryDataStorage::isServerAvailable);
if (historyDataReaders != null) {
available = historyDataReaders.stream().anyMatch(HistoryDataReader::isServerAvailable);
}
if (available) {
return ResponseEntity.ok(Message.success());
Expand Down Expand Up @@ -154,8 154,8 @@ public ResponseEntity<Message<MetricsHistoryData>> getMetricHistoryData(
@Parameter(description = "aggregate data calc. off by default; 4-hour window, query limit >1 week", example = "false")
@RequestParam(required = false) Boolean interval
) {
AbstractHistoryDataStorage historyDataStorage = historyDataStorages.stream()
.filter(AbstractHistoryDataStorage::isServerAvailable).max((o1, o2) -> {
HistoryDataReader historyDataReader = historyDataReaders.stream()
.filter(HistoryDataReader::isServerAvailable).max((o1, o2) -> {
if (o1 instanceof JpaDatabaseDataStorage) {
return -1;
} else if (o2 instanceof JpaDatabaseDataStorage) {
Expand All @@ -164,7 164,7 @@ public ResponseEntity<Message<MetricsHistoryData>> getMetricHistoryData(
return 0;
}
}).orElse(null);
if (historyDataStorage == null) {
if (historyDataReader == null) {
return ResponseEntity.ok(Message.fail(FAIL_CODE, "time series database not available"));
}
String[] names = metricFull.split("\\.");
Expand All @@ -179,9 179,9 @@ public ResponseEntity<Message<MetricsHistoryData>> getMetricHistoryData(
}
Map<String, List<Value>> instanceValuesMap;
if (interval == null || !interval) {
instanceValuesMap = historyDataStorage.getHistoryMetricData(monitorId, app, metrics, metric, label, history);
instanceValuesMap = historyDataReader.getHistoryMetricData(monitorId, app, metrics, metric, label, history);
} else {
instanceValuesMap = historyDataStorage.getHistoryIntervalMetricData(monitorId, app, metrics, metric, label, history);
instanceValuesMap = historyDataReader.getHistoryIntervalMetricData(monitorId, app, metrics, metric, label, history);
}
MetricsHistoryData historyData = MetricsHistoryData.builder()
.id(monitorId).metrics(metrics).values(instanceValuesMap)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 22,7 @@
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.apache.hertzbeat.common.queue.CommonDataQueue;
import org.apache.hertzbeat.warehouse.WarehouseWorkerPool;
import org.apache.hertzbeat.warehouse.store.history.AbstractHistoryDataStorage;
import org.apache.hertzbeat.warehouse.store.history.HistoryDataWriter;
import org.apache.hertzbeat.warehouse.store.history.jpa.JpaDatabaseDataStorage;
import org.apache.hertzbeat.warehouse.store.realtime.AbstractRealTimeDataStorage;
import org.apache.hertzbeat.warehouse.store.realtime.memory.MemoryDataStorage;
Expand All @@ -37,16 37,16 @@ public class DataStorageDispatch {

private final CommonDataQueue commonDataQueue;
private final WarehouseWorkerPool workerPool;
private final List<AbstractHistoryDataStorage> historyDataStorages;
private final List<HistoryDataWriter> historyDataWriters;
private final List<AbstractRealTimeDataStorage> realTimeDataStorages;

public DataStorageDispatch(CommonDataQueue commonDataQueue,
WarehouseWorkerPool workerPool,
List<AbstractHistoryDataStorage> historyDataStorages,
List<HistoryDataWriter> historyDataWriters,
List<AbstractRealTimeDataStorage> realTimeDataStorages) {
this.commonDataQueue = commonDataQueue;
this.workerPool = workerPool;
this.historyDataStorages = historyDataStorages;
this.historyDataWriters = historyDataWriters;
this.realTimeDataStorages = realTimeDataStorages;
startPersistentDataStorage();
startRealTimeDataStorage();
Expand Down Expand Up @@ -80,12 80,12 @@ private void startRealTimeDataStorage() {
}

protected void startPersistentDataStorage() {
if (historyDataStorages == null || historyDataStorages.isEmpty()) {
if (historyDataWriters == null || historyDataWriters.isEmpty()) {
log.info("no history data storage start");
return;
}
if (historyDataStorages.size() > 1) {
historyDataStorages.removeIf(JpaDatabaseDataStorage.class::isInstance);
if (historyDataWriters.size() > 1) {
historyDataWriters.removeIf(JpaDatabaseDataStorage.class::isInstance);
}
Runnable runnable = () -> {
Thread.currentThread().setName("warehouse-persistent-data-storage");
Expand All @@ -95,8 95,8 @@ protected void startPersistentDataStorage() {
if (metricsData == null) {
continue;
}
for (AbstractHistoryDataStorage historyDataStorage : historyDataStorages) {
historyDataStorage.saveData(metricsData);
for (HistoryDataWriter historyDataWriter : historyDataWriters) {
historyDataWriter.saveData(metricsData);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 17,14 @@

package org.apache.hertzbeat.warehouse.store.history;

import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.common.entity.dto.Value;
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.springframework.beans.factory.DisposableBean;

/**
* data storage abstract class
*/
@Slf4j
public abstract class AbstractHistoryDataStorage implements DisposableBean {
public abstract class AbstractHistoryDataStorage implements HistoryDataReader, HistoryDataWriter {

protected boolean serverAvailable;

/**
Expand All @@ -37,37 33,4 @@ public abstract class AbstractHistoryDataStorage implements DisposableBean {
public boolean isServerAvailable() {
return serverAvailable;
}

/**
* save metrics data
* @param metricsData metrics data
*/
public abstract void saveData(CollectRep.MetricsData metricsData);

/**
* query history range metrics data from tsdb
* @param monitorId monitor id
* @param app monitor type
* @param metrics metrics
* @param metric metric
* @param label label
* @param history range
* @return metrics data
*/
public abstract Map<String, List<Value>> getHistoryMetricData(
Long monitorId, String app, String metrics, String metric, String label, String history);

/**
* query history range interval metrics data from tsdb
* max min mean metrics value
* @param monitorId monitor id
* @param app monitor type
* @param metrics metrics
* @param metric metric
* @param label label
* @param history history range
* @return metrics data
*/
public abstract Map<String, List<Value>> getHistoryIntervalMetricData(
Long monitorId, String app, String metrics, String metric, String label, String history);
}
Original file line number Diff line number Diff line change
@@ -0,0 1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hertzbeat.warehouse.store.history;

import java.util.List;
import java.util.Map;
import org.apache.hertzbeat.common.entity.dto.Value;
import org.springframework.beans.factory.DisposableBean;

/**
*
*/
public interface HistoryDataReader extends DisposableBean {

/**
* @return data storage available
*/
boolean isServerAvailable();

/**
* query history range metrics data from tsdb
* @param monitorId monitor id
* @param app monitor type
* @param metrics metrics
* @param metric metric
* @param label label
* @param history range
* @return metrics data
*/
Map<String, List<Value>> getHistoryMetricData(Long monitorId, String app, String metrics, String metric,
String label, String history);

/**
* query history range interval metrics data from tsdb
* max min mean metrics value
* @param monitorId monitor id
* @param app monitor type
* @param metrics metrics
* @param metric metric
* @param label label
* @param history history range
* @return metrics data
*/
Map<String, List<Value>> getHistoryIntervalMetricData(Long monitorId, String app, String metrics, String metric,
String label, String history);
}
Original file line number Diff line number Diff line change
@@ -0,0 1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hertzbeat.warehouse.store.history;

import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.springframework.beans.factory.DisposableBean;

/**
*
*/
public interface HistoryDataWriter extends DisposableBean {

/**
* @return data storage available
*/
boolean isServerAvailable();

/**
* save metrics data
* @param metricsData metrics data
*/
void saveData(CollectRep.MetricsData metricsData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 33,7 @@
import org.apache.hertzbeat.common.constants.CommonConstants;
import org.apache.hertzbeat.common.entity.dto.Value;
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.apache.hertzbeat.warehouse.store.history.AbstractHistoryDataStorage;
import org.apache.hertzbeat.warehouse.store.history.HistoryDataReader;
import org.apache.hertzbeat.warehouse.store.realtime.AbstractRealTimeDataStorage;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -59,33 59,33 @@ class MetricsDataControllerTest {
MetricsDataController metricsDataController;

@Mock
AbstractHistoryDataStorage historyDataStorage;
HistoryDataReader historyDataReader;

@Mock
AbstractRealTimeDataStorage realTimeDataStorage;

private List<AbstractHistoryDataStorage> historyDataStorages = new LinkedList<>();
private List<HistoryDataReader> historyDataReaders = new LinkedList<>();

private List<AbstractRealTimeDataStorage> realTimeDataStorages = new LinkedList<>();

@BeforeEach
void setUp() {
historyDataStorages.add(historyDataStorage);
historyDataReaders.add(historyDataReader);
realTimeDataStorages.add(realTimeDataStorage);
metricsDataController = new MetricsDataController(realTimeDataStorages, historyDataStorages);
metricsDataController = new MetricsDataController(realTimeDataStorages, historyDataReaders);
this.mockMvc = MockMvcBuilders.standaloneSetup(metricsDataController).build();
}

@Test
void getWarehouseStorageServerStatus() throws Exception {
when(historyDataStorage.isServerAvailable()).thenReturn(true);
when(historyDataReader.isServerAvailable()).thenReturn(true);
this.mockMvc.perform(MockMvcRequestBuilders.get("/api/warehouse/storage/status"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andExpect(jsonPath("$.data").isEmpty())
.andExpect(jsonPath("$.msg").isEmpty())
.andReturn();
when(historyDataStorage.isServerAvailable()).thenReturn(false);
when(historyDataReader.isServerAvailable()).thenReturn(false);
this.mockMvc.perform(MockMvcRequestBuilders.get("/api/warehouse/storage/status"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.FAIL_CODE))
Expand Down Expand Up @@ -150,15 150,15 @@ void getMetricHistoryData() throws Exception {
params.add("history", history);
params.add("interval", interval);

when(historyDataStorage.isServerAvailable()).thenReturn(false);
when(historyDataReader.isServerAvailable()).thenReturn(false);
this.mockMvc.perform(MockMvcRequestBuilders.get(getUrl).params(params))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value((int) CommonConstants.FAIL_CODE))
.andExpect(jsonPath("$.msg").value("time series database not available"))
.andExpect(jsonPath("$.data").isEmpty())
.andReturn();

when(historyDataStorage.isServerAvailable()).thenReturn(true);
when(historyDataReader.isServerAvailable()).thenReturn(true);
ServletException exception = assertThrows(ServletException.class, () -> {
this.mockMvc.perform(MockMvcRequestBuilders.get(getUrlFail).params(params))
.andExpect(status().isOk())
Expand All @@ -171,8 171,8 @@ void getMetricHistoryData() throws Exception {
final Map<String, List<Value>> instanceValuesMap = new HashMap<>();
List<Value> list = new ArrayList<>();
instanceValuesMap.put(metric, list);
when(historyDataStorage.isServerAvailable()).thenReturn(true);
lenient().when(historyDataStorage.getHistoryMetricData(eq(monitorId), eq(app), eq(metrics), eq(metric),
when(historyDataReader.isServerAvailable()).thenReturn(true);
lenient().when(historyDataReader.getHistoryMetricData(eq(monitorId), eq(app), eq(metrics), eq(metric),
eq(instance), eq(history)))
.thenReturn(instanceValuesMap);
this.mockMvc.perform(MockMvcRequestBuilders.get(getUrl).params(params))
Expand Down
Loading