Skip to content

Commit

Permalink
uniform config lastmodified ts & refactor config disk storage. (#10533)
Browse files Browse the repository at this point in the history
* uniform cache timestamp

* uniform cache timestamp

* uniform cache timestamp

* uniform cache timestamp

* checkstyle & pmd

* revert application properties

* remove duplicate code

* checkstyle and pmd

* bug fix
  • Loading branch information
shiyiyue1102 committed May 26, 2023
1 parent cf59d62 commit 505c90b
Show file tree
Hide file tree
Showing 46 changed files with 3,377 additions and 1,139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class ConfigChangeClusterSyncRequest extends AbstractConfigRequest {

boolean isBeta;

boolean isBatch;

public boolean isBeta() {
return isBeta;
}
Expand All @@ -46,6 +48,14 @@ public void setBeta(boolean beta) {
isBeta = beta;
}

public boolean isBatch() {
return isBatch;
}

public void setBatch(boolean batch) {
isBatch = batch;
}

/**
* Getter method for property <tt>dataId</tt>.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,34 @@ public static <T> T getOnlyElement(Iterable<T> iterable) {
throw new IllegalArgumentException(buildExceptionMessage(iterator, first));
}

/**
* check list is equal.
*
* @param firstList first list.
* @param secondList second list.
* @return
*/
public static boolean isListEqual(List<String> firstList, List<String> secondList) {
if (firstList == null && secondList == null) {
return true;
}
if (firstList == null || secondList == null) {
return false;
}

if (firstList == secondList) {
return true;
}

if (firstList.size() != secondList.size()) {
return false;
}

boolean flag1 = firstList.containsAll(secondList);
boolean flag2 = secondList.containsAll(firstList);
return flag1 && flag2;
}

@SuppressWarnings("PMD.UndefineMagicConstantRule")
private static <T> String buildExceptionMessage(Iterator<T> iterator, T first) {
StringBuilder msg = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.alibaba.nacos.common.utils;

import java.net.InetAddress;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -27,11 +28,12 @@
*/
@SuppressWarnings({"checkstyle:AbbreviationAsWordInName", "PMD.ClassNamingShouldBeCamelRule"})
public class InternetAddressUtil {

private InternetAddressUtil() {
}

public static final boolean PREFER_IPV6_ADDRESSES = Boolean.parseBoolean(System.getProperty("java.net.preferIPv6Addresses"));

public static final boolean PREFER_IPV6_ADDRESSES = Boolean.parseBoolean(
System.getProperty("java.net.preferIPv6Addresses"));

public static final String IPV6_START_MARK = "[";

Expand All @@ -53,15 +55,17 @@ private InternetAddressUtil() {

private static final String CHECK_OK = "ok";

private static final Pattern DOMAIN_PATTERN = Pattern.compile("[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\\.?");
private static final Pattern DOMAIN_PATTERN = Pattern.compile(
"[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\\.?");

private static final String IPV4_TUPLE = "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])";

private static final Pattern IPV4_PATTERN = Pattern
.compile("(?<!\\d)" + IPV4_TUPLE + "\\." + IPV4_TUPLE + "\\." + IPV4_TUPLE + "\\." + IPV4_TUPLE + "(?!\\d)");
private static final Pattern IPV4_PATTERN = Pattern.compile(
"(?<!\\d)" + IPV4_TUPLE + "\\." + IPV4_TUPLE + "\\." + IPV4_TUPLE + "\\." + IPV4_TUPLE + "(?!\\d)");

/**
* get localhost ip.
*
* @return java.lang.String
*/
public static String localHostIP() {
Expand Down Expand Up @@ -102,8 +106,8 @@ public static boolean isIP(String addr) {
}

/**
* Check if the address contains a port.
* 2020/9/3 14:53
* Check if the address contains a port. 2020/9/3 14:53
*
* @param address address string
* @return boolean
*/
Expand All @@ -112,8 +116,8 @@ public static boolean containsPort(String address) {
}

/**
* Split IP and port strings, support IPv4 and IPv6, IP in IPv6 must be enclosed with [].
* Illegal IP will get abnormal results.
* Split IP and port strings, support IPv4 and IPv6, IP in IPv6 must be enclosed with []. Illegal IP will get
* abnormal results.
*
* @param str ip and port string
* @return java.lang.String[]
Expand All @@ -140,6 +144,7 @@ public static String[] splitIPPortStr(String str) {

/**
* Resolve the IP from the string containing the IP address.
*
* @param str string containing IP address
* @return java.lang.String
*/
Expand All @@ -148,7 +153,8 @@ public static String getIPFromString(String str) {
return "";
}
String result = "";
if (StringUtils.containsIgnoreCase(str, IPV6_START_MARK) && StringUtils.containsIgnoreCase(str, IPV6_END_MARK)) {
if (StringUtils.containsIgnoreCase(str, IPV6_START_MARK) && StringUtils.containsIgnoreCase(str,
IPV6_END_MARK)) {
result = str.substring(str.indexOf(IPV6_START_MARK), (str.indexOf(IPV6_END_MARK) + 1));
if (!isIPv6(result)) {
result = "";
Expand Down Expand Up @@ -192,6 +198,7 @@ public static String checkIPs(String... ips) {

/**
* Check whether checkIPs result is "ok".
*
* @param checkIPsResult checkIPs result
* @return boolean
*/
Expand All @@ -213,7 +220,7 @@ public static String removeBrackets(String str) {
}

/**
* judge str is right domain.(Check only rule)
* judge str is right domain.(Check only rule).
*
* @param str nacosIP
* @return nacosIP is domain
Expand All @@ -228,4 +235,45 @@ public static boolean isDomain(String str) {
return DOMAIN_PATTERN.matcher(str).matches();
}

/**
* convert ip address to int.
*
* @param ip ip address.
* @return int
*/
public static int ipToInt(String ip) {
try {
return bytesToInt(ipToBytesByInet(ip));
} catch (Exception e) {
throw new IllegalArgumentException(ip + " is invalid IP");
}
}

/**
* convert byte array to int.
*
* @param bytes byte array.
* @return int
*/
public static int bytesToInt(byte[] bytes) {
int addr = bytes[3] & 0xFF;
addr |= ((bytes[2] << 8) & 0xFF00);
addr |= ((bytes[1] << 16) & 0xFF0000);
addr |= ((bytes[0] << 24) & 0xFF000000);
return addr;
}

/**
* convert ip address to byte array.
*
* @param ip ip address.
* @return byte[]
*/
public static byte[] ipToBytesByInet(String ip) {
try {
return InetAddress.getByName(ip).getAddress();
} catch (Exception e) {
throw new IllegalArgumentException(ip + " is invalid IP");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.alibaba.nacos.config.server.constant;

import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.config.server.model.event.ConfigDumpEvent;

/**
Expand All @@ -35,13 +36,11 @@ public class Constants {

/**
* Config file directory in server side.
*
*/
public static final String BASE_DIR = "config-data";

/**
* Back up file directory in server side.
*
*/
public static final String CONFIG_BAK_DIR = System.getProperty("user.home", "/home/admin") + "/nacos/bak_data";

Expand Down Expand Up @@ -139,6 +138,12 @@ public class Constants {

public static final String ENCODE = "UTF-8";

public static final String PERSIST_ENCODE = getPersistEncode();

public static final String ENCODE_GBK = "GBK";

public static final String ENCODE_UTF8 = "UTF-8";

public static final String MAP_FILE = "map-file.js";

public static final int FLOW_CONTROL_THRESHOLD = 20;
Expand Down Expand Up @@ -284,4 +289,22 @@ public class Constants {
public static final String CONFIG_SEARCH_BLUR = "blur";

public static final String CONFIG_SEARCH_ACCURATE = "accurate";

/**
* default nacos encode.
*/
public static final String DEFAULT_NACOS_ENCODE = "UTF-8";

public static final String NACOS_PERSIST_ENCODE_KEY = "nacosPersistEncodingKey";

static String getPersistEncode() {
String persistEncode = System.getenv(NACOS_PERSIST_ENCODE_KEY);
if (StringUtils.isBlank(persistEncode)) {
persistEncode = System.getProperty(NACOS_PERSIST_ENCODE_KEY);
if (StringUtils.isBlank(persistEncode)) {
persistEncode = DEFAULT_NACOS_ENCODE;
}
}
return persistEncode;
}
}

0 comments on commit 505c90b

Please sign in to comment.