Skip to content

Commit

Permalink
Merge branch 'master' into fixbug-required-field-check
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangshenghang committed May 23, 2024
2 parents 88063d7 + 7a670e8 commit 89ee415
Show file tree
Hide file tree
Showing 46 changed files with 462 additions and 202 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/doc-build-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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.

name: DOC CI

on:
push:
branches: [ master, dev]
paths:
- 'home/**'
pull_request:
branches: [ master, dev]
paths:
- 'home/**'

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: NPM INSTALL
working-directory: home
run: npm install
- name: NPM BUILD
working-directory: home
run: npm run build
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,18 @@ public Optional<C> getCache(T key, boolean refreshCache) {
cacheMap.remove(key);
return Optional.empty();
}
C value = cacheMap.get(key);
if (value == null) {
log.error("[connection common cache] value is null, remove it, key {}.", key);
cacheMap.remove(key);
timeoutMap.remove(key);
} else if (refreshCache) {
cacheTime[0] = System.currentTimeMillis();
timeoutMap.put(key, cacheTime);
}
C value = cacheMap.compute(key, (k, v) -> {
if (v == null) {
log.error("[connection common cache] value is null, remove it, key {}.", key);
timeoutMap.remove(key);
return null;
}
if (refreshCache) {
cacheTime[0] = System.currentTimeMillis();
timeoutMap.put(key, cacheTime);
}
return v;
});
return Optional.ofNullable(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ public class HttpCollectImpl extends AbstractCollect {
HttpStatus.SC_ACCEPTED, HttpStatus.SC_MULTIPLE_CHOICES, HttpStatus.SC_MOVED_PERMANENTLY,
HttpStatus.SC_MOVED_TEMPORARILY).collect(Collectors.toSet());

public HttpCollectImpl() {
}

@Override
public void preCheck(Metrics metrics) throws IllegalArgumentException {
if (metrics == null || metrics.getHttp() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ public class SslCertificateCollectImpl extends AbstractCollect {
private static final String NAME_END_TIME = "end_time";
private static final String NAME_END_TIMESTAMP = "end_timestamp";

public SslCertificateCollectImpl() {}

@Override
public void preCheck(Metrics metrics) throws IllegalArgumentException {
if (metrics == null || metrics.getHttp() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.apache.hertzbeat.collector.collect.AbstractCollect;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.DiscoveryClient;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.DiscoveryClientManagement;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.ServerInfo;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.entity.ServerInfo;
import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
import org.apache.hertzbeat.common.constants.CollectorConstants;
import org.apache.hertzbeat.common.constants.CommonConstants;
Expand All @@ -43,7 +43,7 @@
*/
@Slf4j
public class HttpsdImpl extends AbstractCollect {
private static final String SERVER = "server";
private static final String SERVER = "server";

@Setter
@VisibleForTesting
Expand Down Expand Up @@ -115,7 +115,7 @@ private void addColumnIfMatched(String fieldName, Object sourceObj, CollectRep.V
try {
Field declaredField = sourceObj.getClass().getDeclaredField(fieldName);
declaredField.setAccessible(Boolean.TRUE);
columnValue = (String) declaredField.get(sourceObj);
columnValue = String.valueOf(declaredField.get(sourceObj));
} catch (NoSuchFieldException | IllegalAccessException e) {
log.warn("No such field for {}", fieldName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.collector.collect.httpsd.constant;

/**
* Discovery client instance status.
*/
public final class DiscoveryClientHealthStatus {

/**
* Discovery client instance status is UP.
*/
public static final String UP = "UP";

/**
* Discovery client instance status is DOWN.
*/
public static final String DOWN = "DOWN";

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
import org.apache.commons.lang3.StringUtils;

/**
* Discovery Client Instance Name For Httpsd monitor
* Discovery Client Instance Name For Http_sd monitor
*/
public enum DiscoveryClientInstance {
CONSUL("Consul"),
NACOS("Nacos"),
NOT_SUPPORT("Not support discovery client instance! "),
;
NOT_SUPPORT("Not support discovery client instance!");

private final String name;

Expand All @@ -43,4 +42,5 @@ public static DiscoveryClientInstance getByName(String clientInstanceName) {
.findFirst()
.orElse(DiscoveryClientInstance.NOT_SUPPORT);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,45 @@
package org.apache.hertzbeat.collector.collect.httpsd.discovery;

import java.util.List;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.entity.ConnectConfig;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.entity.ServerInfo;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.entity.ServiceInstance;
import org.apache.hertzbeat.common.entity.job.protocol.HttpsdProtocol;

/**
* DiscoveryClient interface
* DiscoveryClient interface.
*/
public interface DiscoveryClient extends AutoCloseable {

/**
* Build connect config.
* @param httpsdProtocol httpsd protocol.
* @return connect config object.
*/
ConnectConfig buildConnectConfig(HttpsdProtocol httpsdProtocol);

/**
* Initialize client.
* @param connectConfig connect config.
*/
void initClient(ConnectConfig connectConfig);

/**
* Get server info.
* @return server info object.
*/
ServerInfo getServerInfo();

/**
* Get services.
* @return service instance list.
*/
List<ServiceInstance> getServices();

/**
* Discovery client Health check.
* @return true if health check pass, otherwise false
*/
boolean healthCheck();

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,10 @@ private DiscoveryClient createClient(HttpsdProtocol httpsdProtocol, DiscoveryCli
private DiscoveryClient doCreateClient(HttpsdProtocol httpsdProtocol, DiscoveryClientInstance discoveryClientInstance) {
DiscoveryClient discoveryClient;
switch (discoveryClientInstance) {
case CONSUL:
discoveryClient = new ConsulDiscoveryClient();
break;
case NACOS:
discoveryClient = new NacosDiscoveryClient();
break;
default:
return null;
case CONSUL -> discoveryClient = new ConsulDiscoveryClient();
case NACOS -> discoveryClient = new NacosDiscoveryClient();
default -> { return null; }
}

discoveryClient.initClient(discoveryClient.buildConnectConfig(httpsdProtocol));
return discoveryClient;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
* under the License.
*/

package org.apache.hertzbeat.collector.collect.httpsd.discovery;
package org.apache.hertzbeat.collector.collect.httpsd.discovery.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

/**
* Discovery Client Connect Config
*
*/
@Data
@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package org.apache.hertzbeat.collector.collect.httpsd.discovery;
package org.apache.hertzbeat.collector.collect.httpsd.discovery.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
* under the License.
*/

package org.apache.hertzbeat.collector.collect.httpsd.discovery;
package org.apache.hertzbeat.collector.collect.httpsd.discovery.entity;

import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -33,6 +34,8 @@ public class ServiceInstance {
private String serviceId;
private String serviceName;
private String address;
private String port;
private double weight;
private Map<String, String> metadata;
private int port;
private String healthStatus;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.ConnectConfig;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.DiscoveryClient;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.ServerInfo;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.ServiceInstance;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.entity.ConnectConfig;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.entity.ServerInfo;
import org.apache.hertzbeat.collector.collect.httpsd.discovery.entity.ServiceInstance;
import org.apache.hertzbeat.common.entity.job.protocol.HttpsdProtocol;

/**
* DiscoveryClient impl of Consul
*
*/
public class ConsulDiscoveryClient implements DiscoveryClient {
private ConsulClient consulClient;
Expand Down Expand Up @@ -73,15 +72,33 @@ public List<ServiceInstance> getServices() {
.serviceId(serviceId)
.serviceName(instance.getService())
.address(instance.getAddress())
.port(String.valueOf(instance.getPort()))
.port(instance.getPort())
.metadata(instance.getMeta())
.healthStatus(getHealthStatus(serviceId, healthCheckList))
.build()));

return serviceInstanceList;
}

@Override
public boolean healthCheck() {

List<com.ecwid.consul.v1.health.model.Check> consulHealth = consulClient.getHealthChecksForNode("consul", null)
.getValue();
for (com.ecwid.consul.v1.health.model.Check check : consulHealth) {
com.ecwid.consul.v1.health.model.Check.CheckStatus status = check.getStatus();
if (status != com.ecwid.consul.v1.health.model.Check.CheckStatus.PASSING) {
return false;
}
}

return true;
}

@Override
public void close() {

consulClient = null;
}

private String getHealthStatus(String serviceId, Collection<Check> healthCheckList) {
Expand All @@ -91,4 +108,5 @@ private String getHealthStatus(String serviceId, Collection<Check> healthCheckLi
.map(check -> check.getStatus().name())
.orElse("");
}

}
Loading

0 comments on commit 89ee415

Please sign in to comment.