Skip to content

Commit

Permalink
Merge branch 'master' into feat-hot-load-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuTianyou authored Jul 7, 2024
2 parents a0f8113 + 78621f2 commit 9322f3a
Show file tree
Hide file tree
Showing 30 changed files with 1,692 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.

org.apache.hertzbeat.alert.config.AlerterAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.

org.apache.hertzbeat.collector.config.CollectorAutoConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.protobuf.Message;
import com.google.protobuf.util.JsonFormat;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;

/**
Expand All @@ -39,6 +40,12 @@ private ProtoJsonUtil() {
* @return json
*/
public static String toJsonStr(Message proto) {

if (Objects.isNull(proto)) {
log.error("proto is null");
return null;
}

try {
return PRINTER.print(proto);
} catch (Exception e) {
Expand All @@ -54,6 +61,12 @@ public static String toJsonStr(Message proto) {
* @return protobuf
*/
public static Message toProtobuf(String json, Message.Builder builder) {

if (Objects.isNull(json) || Objects.isNull(builder)) {
log.error("json or builder is null");
return null;
}

try {
PARSER.merge(json, builder);
return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import java.time.Duration;
import java.time.Period;
import java.time.temporal.TemporalAmount;
import lombok.extern.slf4j.Slf4j;

/**
* time util
*/
@Slf4j
public final class TimePeriodUtil {

private TimePeriodUtil() {
Expand All @@ -35,6 +37,12 @@ private TimePeriodUtil() {
* @return TemporalAmount
*/
public static TemporalAmount parseTokenTime(String tokenTime) {

if (tokenTime == null || tokenTime.length() < 2) {
log.error("tokenTime is invalid");
return null;
}

if (Character.isUpperCase(tokenTime.charAt(tokenTime.length() - 1))) {
return Period.parse("P" + tokenTime);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.common.util;

import java.util.Locale;
import java.util.TimeZone;
import org.apache.commons.lang3.StringUtils;
import org.apache.hertzbeat.common.constants.CommonConstants;

/**
* timezone util
*/
public class TimeZoneUtil {
private static final Integer LANG_REGION_LENGTH = 2;

public static void setTimeZoneAndLocale(String timeZoneId, String locale) {
setTimeZone(timeZoneId);
setLocale(locale);
}

public static void setTimeZone(String timeZoneId) {
if (StringUtils.isBlank(timeZoneId)) {
return;
}

TimeZone.setDefault(TimeZone.getTimeZone(timeZoneId));
}

public static void setLocale(String locale) {
if (StringUtils.isBlank(locale)) {
return;
}

String[] arr = locale.split(CommonConstants.LOCALE_SEPARATOR);
if (arr.length == LANG_REGION_LENGTH) {
String language = arr[0];
String country = arr[1];
Locale.setDefault(new Locale(language, country));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.

org.apache.hertzbeat.common.config.CommonConfig
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,60 @@

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

/**
* Test case for {@link LruHashMap}
*/
class LruHashMapTest {

@Test
void removeEldestEntry() {
void testLruHashMap() {

int initThreshold = 3;
LruHashMap<Integer, String> initLruMap = new LruHashMap<>(initThreshold);
assertNotNull(initLruMap);
assertEquals(0, initLruMap.size());

int putAndGetThreshold = 2;
LruHashMap<Integer, String> putAndGetLruMap = new LruHashMap<>(putAndGetThreshold);

putAndGetLruMap.put(1, "one");
putAndGetLruMap.put(2, "two");

// Both entries should be present
assertEquals("one", putAndGetLruMap.get(1));
assertEquals("two", putAndGetLruMap.get(2));

int evictionThreshold = 2;
LruHashMap<Integer, String> evictionLruMap = new LruHashMap<>(evictionThreshold);

evictionLruMap.put(1, "one");
evictionLruMap.put(2, "two");
evictionLruMap.put(3, "three");

// The least recently used entry (1, "one") should be evicted
assertNull(evictionLruMap.get(1));
assertEquals("two", evictionLruMap.get(2));
assertEquals("three", evictionLruMap.get(3));

int accessOrderThreshold = 2;
LruHashMap<Integer, String> accessOrderLruMap = new LruHashMap<>(accessOrderThreshold);

accessOrderLruMap.put(1, "one");
accessOrderLruMap.put(2, "two");

// Access the first entry to make it recently used
accessOrderLruMap.get(1);

accessOrderLruMap.put(3, "three");

// The least recently used entry (2, "two") should be evicted
assertEquals("one", accessOrderLruMap.get(1));
assertNull(accessOrderLruMap.get(2));
assertEquals("three", accessOrderLruMap.get(3));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.common.util;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Test for {@link MapCapUtil}
*/
class MapCapUtilTest {

@Test
public void testCalInitMap() {
int size = 0;
int expectedCapacity = (int) Math.ceil(size / 0.75);
int actualCapacity = MapCapUtil.calInitMap(size);

assertEquals(expectedCapacity, actualCapacity);

size = 10;
expectedCapacity = (int) Math.ceil(size / 0.75);
actualCapacity = MapCapUtil.calInitMap(size);

assertEquals(expectedCapacity, actualCapacity);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,59 @@

package org.apache.hertzbeat.common.util;

import org.apache.hertzbeat.common.util.entity.PersonTest.Person;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

/**
* Test case for {@link ProtoJsonUtil}
*/
class ProtoJsonUtilTest {

private Person samplePerson;
private String sampleJson;

@BeforeEach
void setUp() {

samplePerson = Person.newBuilder()
.setName("John Doe")
.setId(123)
.setEmail("[email protected]")
.build();

sampleJson = "{ \"name\": \"John Doe\", \"id\": 123, \"email\": \"[email protected]\" }";
}

@Test
void toJsonStr() {
void toJsonStr() throws InvalidProtocolBufferException {

String json = ProtoJsonUtil.toJsonStr(samplePerson);
String expectedJson = JsonFormat.printer().print(samplePerson);
assertEquals(expectedJson, json);

json = ProtoJsonUtil.toJsonStr(null);
assertNull(json);
}

@Test
void toProtobuf() {

Person.Builder builder = Person.newBuilder();
Person person = (Person) ProtoJsonUtil.toProtobuf(sampleJson, builder);
assertEquals(samplePerson, person);

String invalidJson = "{ \"name\": \"John Doe\", \"id\": \"not-a-number\" }";
builder = Person.newBuilder();
person = (Person) ProtoJsonUtil.toProtobuf(invalidJson, builder);

assertNull(person);
}

}
Loading

0 comments on commit 9322f3a

Please sign in to comment.