Browse Source

improvements due to comments

pull/8212/head
YevhenBondarenko 4 years ago
parent
commit
32278beabc
  1. 68
      application/src/main/java/org/thingsboard/server/service/system/DefaultSystemInfoService.java
  2. 37
      common/queue/src/main/java/org/thingsboard/server/queue/discovery/DefaultTbServiceInfoProvider.java
  3. 2
      common/queue/src/main/java/org/thingsboard/server/queue/discovery/DiscoveryService.java
  4. 5
      common/queue/src/main/java/org/thingsboard/server/queue/discovery/DummyDiscoveryService.java
  5. 7
      common/queue/src/main/java/org/thingsboard/server/queue/discovery/ZkDiscoveryService.java
  6. 43
      common/util/src/main/java/org/thingsboard/common/util/SystemUtil.java

68
application/src/main/java/org/thingsboard/server/service/system/DefaultSystemInfoService.java

@ -19,7 +19,6 @@ import com.google.common.util.concurrent.FutureCallback;
import com.google.protobuf.ProtocolStringList; import com.google.protobuf.ProtocolStringList;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.common.util.ThingsBoardThreadFactory; import org.thingsboard.common.util.ThingsBoardThreadFactory;
@ -82,19 +81,20 @@ public class DefaultSystemInfoService extends TbApplicationEventListener<Partiti
private final DiscoveryService discoveryService; private final DiscoveryService discoveryService;
private final TelemetrySubscriptionService telemetryService; private final TelemetrySubscriptionService telemetryService;
private final TbApiUsageStateClient apiUsageStateClient; private final TbApiUsageStateClient apiUsageStateClient;
private ScheduledExecutorService scheduler; private volatile ScheduledExecutorService scheduler;
@Value("${zk.enabled:false}")
private boolean zkEnabled;
@Override @Override
protected void onTbApplicationEvent(PartitionChangeEvent partitionChangeEvent) { protected void onTbApplicationEvent(PartitionChangeEvent partitionChangeEvent) {
if (ServiceType.TB_CORE.equals(partitionChangeEvent.getServiceType())) { if (ServiceType.TB_CORE.equals(partitionChangeEvent.getServiceType())) {
if (scheduler == null && partitionService.resolve(ServiceType.TB_CORE, TenantId.SYS_TENANT_ID, TenantId.SYS_TENANT_ID).isMyPartition()) { synchronized (this) {
scheduler = Executors.newSingleThreadScheduledExecutor(ThingsBoardThreadFactory.forName("tb-system-info-scheduler")); if (partitionService.resolve(ServiceType.TB_CORE, TenantId.SYS_TENANT_ID, TenantId.SYS_TENANT_ID).isMyPartition()) {
scheduler.scheduleAtFixedRate(this::saveCurrentSystemInfo, 0, 1, TimeUnit.MINUTES); if (scheduler == null) {
} else { scheduler = Executors.newSingleThreadScheduledExecutor(ThingsBoardThreadFactory.forName("tb-system-info-scheduler"));
destroy(); scheduler.scheduleAtFixedRate(this::saveCurrentSystemInfo, 0, 1, TimeUnit.MINUTES);
}
} else {
destroy();
}
} }
} }
} }
@ -105,27 +105,26 @@ public class DefaultSystemInfoService extends TbApplicationEventListener<Partiti
ServiceInfo serviceInfo = serviceInfoProvider.getServiceInfoWithCurrentSystemInfo(); ServiceInfo serviceInfo = serviceInfoProvider.getServiceInfoWithCurrentSystemInfo();
if (zkEnabled) { if (discoveryService.isMonolith()) {
systemInfo.setSystemData(getSystemData(serviceInfo));
} else {
systemInfo.setMonolith(true); systemInfo.setMonolith(true);
systemInfo.setSystemData(Collections.singletonList(createSystemInfoData(serviceInfo))); systemInfo.setSystemData(Collections.singletonList(createSystemInfoData(serviceInfo)));
} else {
systemInfo.setSystemData(getSystemData(serviceInfo));
} }
return systemInfo; return systemInfo;
} }
protected void saveCurrentSystemInfo() { protected void saveCurrentSystemInfo() {
if (zkEnabled) { if (discoveryService.isMonolith()) {
saveCurrentClusterSystemInfo();
} else {
saveCurrentMonolithSystemInfo(); saveCurrentMonolithSystemInfo();
} else {
saveCurrentClusterSystemInfo();
} }
} }
private void saveCurrentClusterSystemInfo() { private void saveCurrentClusterSystemInfo() {
long ts = System.currentTimeMillis(); long ts = System.currentTimeMillis();
List<SystemInfoData> clusterSystemData = getSystemData(serviceInfoProvider.getServiceInfoWithCurrentSystemInfo()); List<SystemInfoData> clusterSystemData = getSystemData(serviceInfoProvider.getServiceInfoWithCurrentSystemInfo());
BasicTsKvEntry clusterDataKv = new BasicTsKvEntry(ts, new JsonDataEntry("clusterSystemData", JacksonUtil.toString(clusterSystemData))); BasicTsKvEntry clusterDataKv = new BasicTsKvEntry(ts, new JsonDataEntry("clusterSystemData", JacksonUtil.toString(clusterSystemData)));
doSave(Collections.singletonList(clusterDataKv)); doSave(Collections.singletonList(clusterDataKv));
@ -135,34 +134,13 @@ public class DefaultSystemInfoService extends TbApplicationEventListener<Partiti
long ts = System.currentTimeMillis(); long ts = System.currentTimeMillis();
List<TsKvEntry> tsList = new ArrayList<>(); List<TsKvEntry> tsList = new ArrayList<>();
Long memoryUsage = getMemoryUsage(); getMemoryUsage().ifPresent(v -> tsList.add(new BasicTsKvEntry(ts, new LongDataEntry("memoryUsage", v))));
if (memoryUsage != null) { getTotalMemory().ifPresent(v -> tsList.add(new BasicTsKvEntry(ts, new LongDataEntry("totalMemory", v))));
tsList.add(new BasicTsKvEntry(ts, new LongDataEntry("memoryUsage", memoryUsage))); getFreeMemory().ifPresent(v -> tsList.add(new BasicTsKvEntry(ts, new LongDataEntry("freeMemory", v))));
} getCpuUsage().ifPresent(v -> tsList.add(new BasicTsKvEntry(ts, new DoubleDataEntry("cpuUsage", v))));
Long totalMemory = getTotalMemory(); getTotalCpuUsage().ifPresent(v -> tsList.add(new BasicTsKvEntry(ts, new DoubleDataEntry("totalCpuUsage", v))));
if (totalMemory != null) { getFreeDiscSpace().ifPresent(v -> tsList.add(new BasicTsKvEntry(ts, new LongDataEntry("freeDiscSpace", v))));
tsList.add(new BasicTsKvEntry(ts, new LongDataEntry("totalMemory", totalMemory))); getTotalDiscSpace().ifPresent(v -> tsList.add(new BasicTsKvEntry(ts, new LongDataEntry("totalDiscSpace", v))));
}
Long freeMemory = getFreeMemory();
if (freeMemory != null) {
tsList.add(new BasicTsKvEntry(ts, new LongDataEntry("freeMemory", freeMemory)));
}
Double cpuUsage = getCpuUsage();
if (cpuUsage != null) {
tsList.add(new BasicTsKvEntry(ts, new DoubleDataEntry("cpuUsage", cpuUsage)));
}
Double totalCpuUsage = getTotalCpuUsage();
if (totalCpuUsage != null) {
tsList.add(new BasicTsKvEntry(ts, new DoubleDataEntry("totalCpuUsage", totalCpuUsage)));
}
Long freeDiscSpace = getFreeDiscSpace();
if (freeDiscSpace != null) {
tsList.add(new BasicTsKvEntry(ts, new LongDataEntry("freeDiscSpace", freeDiscSpace)));
}
Long totalDiscSpace = getTotalDiscSpace();
if (totalDiscSpace != null) {
tsList.add(new BasicTsKvEntry(ts, new LongDataEntry("totalDiscSpace", totalDiscSpace)));
}
doSave(tsList); doSave(tsList);
} }

37
common/queue/src/main/java/org/thingsboard/server/queue/discovery/DefaultTbServiceInfoProvider.java

@ -37,9 +37,9 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.thingsboard.common.util.SystemUtil.getFreeMemory;
import static org.thingsboard.common.util.SystemUtil.getCpuUsage; import static org.thingsboard.common.util.SystemUtil.getCpuUsage;
import static org.thingsboard.common.util.SystemUtil.getFreeDiscSpace; import static org.thingsboard.common.util.SystemUtil.getFreeDiscSpace;
import static org.thingsboard.common.util.SystemUtil.getFreeMemory;
import static org.thingsboard.common.util.SystemUtil.getMemoryUsage; import static org.thingsboard.common.util.SystemUtil.getMemoryUsage;
import static org.thingsboard.common.util.SystemUtil.getTotalCpuUsage; import static org.thingsboard.common.util.SystemUtil.getTotalCpuUsage;
import static org.thingsboard.common.util.SystemUtil.getTotalDiscSpace; import static org.thingsboard.common.util.SystemUtil.getTotalDiscSpace;
@ -118,34 +118,13 @@ public class DefaultTbServiceInfoProvider implements TbServiceInfoProvider {
private TransportProtos.SystemInfoProto getCurrentSystemInfoProto() { private TransportProtos.SystemInfoProto getCurrentSystemInfoProto() {
TransportProtos.SystemInfoProto.Builder builder = TransportProtos.SystemInfoProto.newBuilder(); TransportProtos.SystemInfoProto.Builder builder = TransportProtos.SystemInfoProto.newBuilder();
Long memoryUsage = getMemoryUsage(); getMemoryUsage().ifPresent(builder::setMemoryUsage);
if (memoryUsage != null) { getTotalMemory().ifPresent(builder::setTotalMemory);
builder.setMemoryUsage(memoryUsage); getFreeMemory().ifPresent(builder::setFreeMemory);
} getCpuUsage().ifPresent(builder::setCpuUsage);
Long totalMemory = getTotalMemory(); getTotalCpuUsage().ifPresent(builder::setTotalCpuUsage);
if (totalMemory != null) { getFreeDiscSpace().ifPresent(builder::setFreeDiscSpace);
builder.setTotalMemory(totalMemory); getTotalDiscSpace().ifPresent(builder::setTotalDiscSpace);
}
Long freeMemory = getFreeMemory();
if (freeMemory != null) {
builder.setFreeMemory(freeMemory);
}
Double cpuUsage = getCpuUsage();
if (cpuUsage != null) {
builder.setCpuUsage(cpuUsage);
}
Double totalCpuUsage = getTotalCpuUsage();
if (totalCpuUsage != null) {
builder.setTotalCpuUsage(totalCpuUsage);
}
Long freeDiscSpace = getFreeDiscSpace();
if (freeDiscSpace != null) {
builder.setFreeDiscSpace(freeDiscSpace);
}
Long totalDiscSpace = getTotalDiscSpace();
if (totalDiscSpace != null) {
builder.setTotalDiscSpace(totalDiscSpace);
}
return builder.build(); return builder.build();
} }

2
common/queue/src/main/java/org/thingsboard/server/queue/discovery/DiscoveryService.java

@ -23,4 +23,6 @@ public interface DiscoveryService {
List<TransportProtos.ServiceInfo> getOtherServers(); List<TransportProtos.ServiceInfo> getOtherServers();
boolean isMonolith();
} }

5
common/queue/src/main/java/org/thingsboard/server/queue/discovery/DummyDiscoveryService.java

@ -52,4 +52,9 @@ public class DummyDiscoveryService implements DiscoveryService {
public List<TransportProtos.ServiceInfo> getOtherServers() { public List<TransportProtos.ServiceInfo> getOtherServers() {
return Collections.emptyList(); return Collections.emptyList();
} }
@Override
public boolean isMonolith() {
return true;
}
} }

7
common/queue/src/main/java/org/thingsboard/server/queue/discovery/ZkDiscoveryService.java

@ -115,6 +115,11 @@ public class ZkDiscoveryService implements DiscoveryService, PathChildrenCacheLi
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@Override
public boolean isMonolith() {
return false;
}
@AfterStartUp(order = AfterStartUp.DISCOVERY_SERVICE) @AfterStartUp(order = AfterStartUp.DISCOVERY_SERVICE)
public void onApplicationEvent(ApplicationReadyEvent event) { public void onApplicationEvent(ApplicationReadyEvent event) {
if (stopped) { if (stopped) {
@ -288,8 +293,6 @@ public class ZkDiscoveryService implements DiscoveryService, PathChildrenCacheLi
case CHILD_REMOVED: case CHILD_REMOVED:
recalculatePartitions(); recalculatePartitions();
break; break;
case CHILD_UPDATED:
break;
default: default:
break; break;
} }

43
common/util/src/main/java/org/thingsboard/common/util/SystemUtil.java

@ -24,6 +24,7 @@ import java.lang.management.MemoryMXBean;
import java.nio.file.FileStore; import java.nio.file.FileStore;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Optional;
@Slf4j @Slf4j
public class SystemUtil { public class SystemUtil {
@ -35,70 +36,70 @@ public class SystemUtil {
HARDWARE = si.getHardware(); HARDWARE = si.getHardware();
} }
public static Long getMemoryUsage() { public static Optional<Long> getMemoryUsage() {
try { try {
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
return memoryMXBean.getHeapMemoryUsage().getUsed(); return Optional.of(memoryMXBean.getHeapMemoryUsage().getUsed());
} catch (Exception e) { } catch (Exception e) {
log.debug("Failed to get memory usage!!!", e); log.debug("Failed to get memory usage!!!", e);
} }
return null; return Optional.empty();
} }
public static Long getTotalMemory() { public static Optional<Long> getTotalMemory() {
try { try {
return HARDWARE.getMemory().getTotal(); return Optional.of(HARDWARE.getMemory().getTotal());
} catch (Exception e) { } catch (Exception e) {
log.debug("Failed to get total memory!!!", e); log.debug("Failed to get total memory!!!", e);
} }
return null; return Optional.empty();
} }
public static Long getFreeMemory() { public static Optional<Long> getFreeMemory() {
try { try {
return HARDWARE.getMemory().getAvailable(); return Optional.of(HARDWARE.getMemory().getAvailable());
} catch (Exception e) { } catch (Exception e) {
log.debug("Failed to get free memory!!!", e); log.debug("Failed to get free memory!!!", e);
} }
return null; return Optional.empty();
} }
public static Double getCpuUsage() { public static Optional<Double> getCpuUsage() {
try { try {
return prepare(HARDWARE.getProcessor().getSystemLoadAverage()); return Optional.of(prepare(HARDWARE.getProcessor().getSystemLoadAverage()));
} catch (Exception e) { } catch (Exception e) {
log.debug("Failed to get cpu usage!!!", e); log.debug("Failed to get cpu usage!!!", e);
} }
return null; return Optional.empty();
} }
public static Double getTotalCpuUsage() { public static Optional<Double> getTotalCpuUsage() {
try { try {
return prepare(HARDWARE.getProcessor().getSystemCpuLoad() * 100); return Optional.of(prepare(HARDWARE.getProcessor().getSystemCpuLoad() * 100));
} catch (Exception e) { } catch (Exception e) {
log.debug("Failed to get total cpu usage!!!", e); log.debug("Failed to get total cpu usage!!!", e);
} }
return null; return Optional.empty();
} }
public static Long getFreeDiscSpace() { public static Optional<Long> getFreeDiscSpace() {
try { try {
FileStore store = Files.getFileStore(Paths.get("/")); FileStore store = Files.getFileStore(Paths.get("/"));
return store.getUsableSpace(); return Optional.of(store.getUsableSpace());
} catch (Exception e) { } catch (Exception e) {
log.debug("Failed to get free disc space!!!", e); log.debug("Failed to get free disc space!!!", e);
} }
return null; return Optional.empty();
} }
public static Long getTotalDiscSpace() { public static Optional<Long> getTotalDiscSpace() {
try { try {
FileStore store = Files.getFileStore(Paths.get("/")); FileStore store = Files.getFileStore(Paths.get("/"));
return store.getTotalSpace(); return Optional.of(store.getTotalSpace());
} catch (Exception e) { } catch (Exception e) {
log.debug("Failed to get total disc space!!!", e); log.debug("Failed to get total disc space!!!", e);
} }
return null; return Optional.empty();
} }
private static Double prepare(Double d) { private static Double prepare(Double d) {

Loading…
Cancel
Save