Skip to content

Commit

Permalink
Develop client ut (alibaba#11557)
Browse files Browse the repository at this point in the history
* Add UT for client module logging package.

* Add UT for client module utils package.

* Add UT for client module naming package.

* For checkstyle.

* For PMD.
  • Loading branch information
KomachiSion authored Dec 26, 2023
1 parent 41222ee commit 9e48e8c
Show file tree
Hide file tree
Showing 63 changed files with 2,354 additions and 960 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,8 @@ public void loadConfiguration() {
try {
nacosLogging.loadConfiguration();
} catch (Throwable t) {
if (isLogback) {
LOGGER.warn("Load Logback Configuration of Nacos fail, message: {}", t.getMessage());
} else {
LOGGER.warn("Load Log4j Configuration of Nacos fail, message: {}", t.getMessage());
}
String loggerName = isLogback ? "Logback" : "Log4j";
LOGGER.warn("Load {} Configuration of Nacos fail, message: {}", loggerName, t.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ public static Gauge.Child getServiceInfoMapSizeMonitor() {
return NACOS_MONITOR.labels("naming", "serviceInfoMapSize");
}

public static Gauge.Child getDom2BeatSizeMonitor() {
return NACOS_MONITOR.labels("naming", "dom2BeatSize");
}

public static Gauge.Child getListenConfigCountMonitor() {
return NACOS_MONITOR.labels("config", "listenConfigCount");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class NacosNamingService implements NamingService {
*/
private String namespace;

@Deprecated
private String logName;

private ServiceInfoHolder serviceInfoHolder;
Expand Down Expand Up @@ -107,6 +108,7 @@ private void init(Properties properties) throws NacosException {
changeNotifier);
}

@Deprecated
private void initLogName(NacosClientProperties properties) {
logName = properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME, DEFAULT_NAMING_LOG_FILE_PATH);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public class FailoverData {
/**
* failover type,naming or config.
*/
private DataType dataType;
private final DataType dataType;

/**
* failover data.
*/
private Object data;
private final Object data;

public FailoverData(DataType dataType, Object data) {
this.data = data;
Expand All @@ -53,15 +53,7 @@ public DataType getDataType() {
return dataType;
}

public void setDataType(DataType dataType) {
this.dataType = dataType;
}

public Object getData() {
return data;
}

public void setData(Object data) {
this.data = data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Metrics;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -60,7 +58,7 @@ public class FailoverReactor implements Closeable {

private String notifierEventScope;

private HashMap<String, Meter> meterMap = new HashMap<>(10);
private Map<String, Meter> meterMap = new HashMap<>(10);

public FailoverReactor(ServiceInfoHolder serviceInfoHolder, String notifierEventScope) {
this.serviceInfoHolder = serviceInfoHolder;
Expand All @@ -85,9 +83,7 @@ public FailoverReactor(ServiceInfoHolder serviceInfoHolder, String notifierEvent
* Init.
*/
public void init() {

executorService.scheduleWithFixedDelay(new FailoverSwitchRefresher(), 0L, 5000L, TimeUnit.MILLISECONDS);

}

class FailoverSwitchRefresher implements Runnable {
Expand Down Expand Up @@ -146,7 +142,6 @@ public void run() {
serviceMap.clear();
failoverSwitchEnable = false;
failoverServiceCntMetricsClear();
return;
}
} catch (Exception e) {
NAMING_LOGGER.error("FailoverSwitchRefresher run err", e);
Expand All @@ -169,20 +164,6 @@ public ServiceInfo getService(String key) {
return serviceInfo;
}

/**
* Add day.
*
* @param date start time
* @param num add day number
* @return new date
*/
public Date addDay(Date date, int num) {
Calendar startDT = Calendar.getInstance();
startDT.setTime(date);
startDT.add(Calendar.DAY_OF_MONTH, num);
return startDT.getTime();
}

/**
* shutdown ThreadPool.
*
Expand All @@ -200,10 +181,10 @@ private void failoverServiceCntMetrics(Map<String, ServiceInfo> failoverMap) {
try {
for (Map.Entry<String, ServiceInfo> entry : failoverMap.entrySet()) {
String serviceName = entry.getKey();
Gauge register = Gauge.builder("nacos_naming_client_failover_instances",
((ServiceInfo) failoverMap.get(serviceName)).ipCount(), Integer::intValue)
.tag("service_name", serviceName).description("Nacos failover data service count")
.register(Metrics.globalRegistry);
Gauge register = Gauge
.builder("nacos_naming_client_failover_instances", failoverMap.get(serviceName).ipCount(),
Integer::intValue).tag("service_name", serviceName)
.description("Nacos failover data service count").register(Metrics.globalRegistry);
meterMap.put(serviceName, register);
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,12 @@ public class FailoverSwitch {
/**
* Failover switch enable.
*/
private boolean enabled;
private final boolean enabled;

public boolean getEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public FailoverSwitch(boolean enabled) {
this.enabled = enabled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,11 @@
import com.alibaba.nacos.client.naming.cache.ConcurrentDiskUtil;
import com.alibaba.nacos.client.naming.cache.DiskCache;
import com.alibaba.nacos.client.naming.utils.CacheDirUtil;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.StringReader;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -66,7 +60,7 @@ public class DiskFailoverDataSource implements FailoverDataSource {
private long lastModifiedMillis = 0L;

public DiskFailoverDataSource() {
failoverDir = CacheDirUtil.gettCacheDir() + FAILOVER_DIR;
failoverDir = CacheDirUtil.getCacheDir() + FAILOVER_DIR;
}

class FailoverFileReader implements Runnable {
Expand All @@ -75,13 +69,9 @@ class FailoverFileReader implements Runnable {
public void run() {
Map<String, FailoverData> domMap = new HashMap<>(200);

BufferedReader reader = null;
try {

File cacheDir = new File(failoverDir);
if (!cacheDir.exists() && !cacheDir.mkdirs()) {
throw new IllegalStateException("failed to create cache dir: " + failoverDir);
}
DiskCache.createFileIfAbsent(cacheDir, true);

File[] files = cacheDir.listFiles();
if (files == null) {
Expand All @@ -97,36 +87,8 @@ public void run() {
continue;
}

ServiceInfo dom = null;

try {
dom = new ServiceInfo(URLDecoder.decode(file.getName(), StandardCharsets.UTF_8.name()));
String dataString = ConcurrentDiskUtil.getFileContent(file,
Charset.defaultCharset().toString());
reader = new BufferedReader(new StringReader(dataString));

String json;
if ((json = reader.readLine()) != null) {
try {
dom = JacksonUtils.toObj(json, ServiceInfo.class);
} catch (Exception e) {
NAMING_LOGGER.error("[NA] error while parsing cached dom : {}", json, e);
}
}

} catch (Exception e) {
NAMING_LOGGER.error("[NA] failed to read cache for dom: {}", file.getName(), e);
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (Exception e) {
//ignore
}
}
if (dom != null && !CollectionUtils.isEmpty(dom.getHosts())) {
domMap.put(dom.getKey(), NamingFailoverData.newNamingFailoverData(dom));
for (Map.Entry<String, ServiceInfo> entry : DiskCache.parseServiceInfoFromCache(file).entrySet()) {
domMap.put(entry.getKey(), NamingFailoverData.newNamingFailoverData(entry.getValue()));
}
}
} catch (Exception e) {
Expand All @@ -152,8 +114,8 @@ public FailoverSwitch getSwitch() {

if (lastModifiedMillis < modified) {
lastModifiedMillis = modified;
String failover = ConcurrentDiskUtil.getFileContent(switchFile.getPath(),
Charset.defaultCharset().toString());
String failover = ConcurrentDiskUtil
.getFileContent(switchFile.getPath(), Charset.defaultCharset().toString());
if (!StringUtils.isEmpty(failover)) {
String[] lines = failover.split(DiskCache.getLineSeparator());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.alibaba.nacos.client.naming.cache;

import com.alibaba.nacos.common.utils.IoUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand Down Expand Up @@ -68,39 +66,14 @@ public static String getFileContent(String path, String charsetName) throws IOEx
* @throws IOException IOException
*/
public static String getFileContent(File file, String charsetName) throws IOException {
RandomAccessFile fis = null;
FileLock rlock = null;
try {
fis = new RandomAccessFile(file, READ_ONLY);
FileChannel fcin = fis.getChannel();
int i = 0;
do {
try {
rlock = fcin.tryLock(0L, Long.MAX_VALUE, true);
} catch (Exception e) {
++i;
if (i > RETRY_COUNT) {
NAMING_LOGGER.error("[NA] read " + file.getName() + " fail;retryed time: " + i, e);
throw new IOException("read " + file.getAbsolutePath() + " conflict");
}
sleep(SLEEP_BASETIME * i);
NAMING_LOGGER.warn("read " + file.getName() + " conflict;retry time: " + i);
}
} while (null == rlock);
try (RandomAccessFile fis = new RandomAccessFile(file, READ_ONLY);
FileChannel fcin = fis.getChannel();
FileLock rlock = tryLock(file, fcin, true)) {
int fileSize = (int) fcin.size();
ByteBuffer byteBuffer = ByteBuffer.allocate(fileSize);
fcin.read(byteBuffer);
byteBuffer.flip();
return byteBufferToString(byteBuffer, charsetName);
} finally {
if (rlock != null) {
rlock.release();
rlock = null;
}
if (fis != null) {
IoUtils.closeQuietly(fis);
fis = null;
}
}
}

Expand Down Expand Up @@ -132,27 +105,9 @@ public static Boolean writeFileContent(File file, String content, String charset
if (!file.exists() && !file.createNewFile()) {
return false;
}
FileChannel channel = null;
FileLock lock = null;
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(file, READ_WRITE);
channel = raf.getChannel();
int i = 0;
do {
try {
lock = channel.tryLock();
} catch (Exception e) {
++i;
if (i > RETRY_COUNT) {
NAMING_LOGGER.error("[NA] write {} fail;retryed time:{}", file.getName(), i);
throw new IOException("write " + file.getAbsolutePath() + " conflict", e);
}
sleep(SLEEP_BASETIME * i);
NAMING_LOGGER.warn("write " + file.getName() + " conflict;retry time: " + i);
}
} while (null == lock);

try (RandomAccessFile raf = new RandomAccessFile(file, READ_WRITE);
FileChannel channel = raf.getChannel();
FileLock lock = tryLock(file, channel, false)) {
byte[] contentBytes = content.getBytes(charsetName);
ByteBuffer sendBuffer = ByteBuffer.wrap(contentBytes);
while (sendBuffer.hasRemaining()) {
Expand All @@ -161,32 +116,6 @@ public static Boolean writeFileContent(File file, String content, String charset
channel.truncate(contentBytes.length);
} catch (FileNotFoundException e) {
throw new IOException("file not exist");
} finally {
if (lock != null) {
try {
lock.release();
lock = null;
} catch (IOException e) {
NAMING_LOGGER.warn("close wrong", e);
}
}
if (channel != null) {
try {
channel.close();
channel = null;
} catch (IOException e) {
NAMING_LOGGER.warn("close wrong", e);
}
}
if (raf != null) {
try {
raf.close();
raf = null;
} catch (IOException e) {
NAMING_LOGGER.warn("close wrong", e);
}
}

}
return true;
}
Expand Down Expand Up @@ -216,4 +145,23 @@ private static void sleep(int time) {
}
}

private static FileLock tryLock(File file, FileChannel channel, boolean shared) throws IOException {
FileLock result = null;
int i = 0;
do {
try {
result = channel.tryLock(0L, Long.MAX_VALUE, shared);
} catch (Exception e) {
++i;
if (i > RETRY_COUNT) {
NAMING_LOGGER.error("[NA] read " + file.getName() + " fail;retryed time: " + i, e);
throw new IOException("read " + file.getAbsolutePath() + " conflict");
}
sleep(SLEEP_BASETIME * i);
NAMING_LOGGER.warn("read " + file.getName() + " conflict;retry time: " + i);
}
} while (null == result);
return result;
}

}
Loading

0 comments on commit 9e48e8c

Please sign in to comment.