create error code

This commit is contained in:
chuntaojun 2020-07-27 13:03:45 +08:00
parent e90ac7e137
commit 018ce1441f
5 changed files with 119 additions and 54 deletions

View File

@ -24,6 +24,7 @@ package com.alibaba.nacos.api.common;
* <li> Global and common code starts with 10001.
* <li> Naming module code starts with 20001.
* <li> Config module code starts with 30001.
* <li> Core module code starts with 40001.
*
* @author nkorange
* @since 1.2.0

View File

@ -0,0 +1,62 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.core.exception;
/**
* Core module code starts with 40001.
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
public enum ErrorCode {
UnKnowError(40001),
RocksDBWriteError(40100),
RocksDBReadError(40101),
RocksDBDeleteError(40102),
RocksDBSnapshotSaveError(40103),
RocksDBSnapshotLoadError(40104),
RocksDBResetError(40105),
RocksDBCreateError(40106),
// disk error
IOMakeDirError(40201),
// consistency protocol
ProtoSubmitError(40301),
ProtoReadError(40302),
;
private final int code;
ErrorCode(int code) {
this.code = code;
}
public int getCode() {
return code;
}
}

View File

@ -17,10 +17,10 @@
package com.alibaba.nacos.core.storage;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.core.exception.ErrorCode;
import com.alibaba.nacos.core.exception.RocksStorageException;
import com.alibaba.nacos.core.utils.DiskUtils;
import org.rocksdb.BackupEngine;
@ -53,63 +53,63 @@ import java.util.stream.Collectors;
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
public final class RocksStorage {
private String group;
private DBOptions options;
private RocksDB db;
private WriteOptions writeOptions;
private ReadOptions readOptions;
private ColumnFamilyHandle defaultHandle;
private String dbPath;
private final List<ColumnFamilyOptions> cfOptions = new ArrayList<>();
static {
RocksDB.loadLibrary();
}
private RocksStorage() {
}
public static RocksStorage createDefault(final String group, String baseDir) {
return createCustomer(group, baseDir, new WriteOptions().setSync(true),
new ReadOptions().setTotalOrderSeek(true));
}
public static RocksStorage createCustomer(final String group, String baseDir, WriteOptions writeOptions,
ReadOptions readOptions) {
RocksStorage storage = new RocksStorage();
try {
DiskUtils.forceMkdir(baseDir);
} catch (IOException e) {
throw new NacosRuntimeException(NacosException.SERVER_ERROR, e);
throw new NacosRuntimeException(ErrorCode.IOMakeDirError.getCode(), e);
}
createRocksDB(baseDir, group, writeOptions, readOptions, storage);
return storage;
}
public void destroyAndOpenNew() throws RocksStorageException {
try (final Options options = new Options()) {
RocksDB.destroyDB(dbPath, options);
createRocksDB(dbPath, group, writeOptions, readOptions, this);
} catch (RocksDBException ex) {
Status status = ex.getStatus();
throw createRocksStorageException(status);
throw createRocksStorageException(ErrorCode.RocksDBResetError, status);
}
}
private static void createRocksDB(final String baseDir, final String group, WriteOptions writeOptions,
ReadOptions readOptions, final RocksStorage storage) {
ReadOptions readOptions, final RocksStorage storage) {
storage.cfOptions.clear();
final DBOptions options = RocksDBUtils.getDefaultRocksDBOptions();
final List<ColumnFamilyHandle> columnFamilyHandles = new ArrayList<>();
final List<ColumnFamilyDescriptor> columnFamilyDescriptors = new ArrayList<>();
@ -126,19 +126,19 @@ public final class RocksStorage {
storage.db = db;
storage.defaultHandle = columnFamilyHandles.get(0);
} catch (RocksDBException e) {
throw new NacosRuntimeException(NacosException.SERVER_ERROR, e);
throw new NacosRuntimeException(ErrorCode.RocksDBCreateError.getCode(), e);
}
}
public void write(byte[] key, byte[] value) throws RocksStorageException {
try {
this.db.put(defaultHandle, writeOptions, key, value);
} catch (RocksDBException e) {
Status status = e.getStatus();
throw createRocksStorageException(status);
throw createRocksStorageException(ErrorCode.RocksDBWriteError, status);
}
}
public void batchWrite(List<byte[]> key, List<byte[]> values) throws RocksStorageException {
if (key.size() != values.size()) {
throw new IllegalArgumentException("key size and values size must be equals!");
@ -150,37 +150,37 @@ public final class RocksStorage {
db.write(writeOptions, batch);
} catch (RocksDBException e) {
Status status = e.getStatus();
throw createRocksStorageException(status);
throw createRocksStorageException(ErrorCode.RocksDBWriteError, status);
}
}
public byte[] get(byte[] key) throws RocksStorageException {
try {
return db.get(defaultHandle, readOptions, key);
} catch (RocksDBException e) {
Status status = e.getStatus();
throw createRocksStorageException(status);
throw createRocksStorageException(ErrorCode.RocksDBReadError, status);
}
}
public Map<byte[], byte[]> batchGet(List<byte[]> key) throws RocksStorageException {
try {
return db.multiGet(readOptions, key);
} catch (RocksDBException e) {
Status status = e.getStatus();
throw createRocksStorageException(status);
throw createRocksStorageException(ErrorCode.RocksDBReadError, status);
}
}
public void delete(byte[] key) throws RocksStorageException {
try {
db.delete(defaultHandle, writeOptions, key);
} catch (RocksDBException e) {
Status status = e.getStatus();
throw createRocksStorageException(status);
throw createRocksStorageException(ErrorCode.RocksDBDeleteError, status);
}
}
public void batchDelete(List<byte[]> key) throws RocksStorageException {
try {
for (byte[] k : key) {
@ -188,10 +188,10 @@ public final class RocksStorage {
}
} catch (RocksDBException e) {
Status status = e.getStatus();
throw createRocksStorageException(status);
throw createRocksStorageException(ErrorCode.RocksDBDeleteError, status);
}
}
public void snapshotSave(final String backupPath) throws RocksStorageException {
final String path = Paths.get(backupPath, group).toString();
Throwable ex = DiskUtils.forceMkdir(path, (aVoid, ioe) -> {
@ -208,15 +208,16 @@ public final class RocksStorage {
return null;
} catch (RocksDBException e) {
Status status = e.getStatus();
return createRocksStorageException(status);
return createRocksStorageException(ErrorCode.RocksDBSnapshotSaveError, status);
} catch (Throwable throwable) {
return throwable;
}
}); if (ex != null) {
throw new RocksStorageException(NacosException.SERVER_ERROR, ex);
});
if (ex != null) {
throw new RocksStorageException(ErrorCode.UnKnowError.getCode(), ex);
}
}
public void snapshotLoad(final String backupPath) throws RocksStorageException {
try {
final String path = Paths.get(backupPath, group).toString();
@ -230,17 +231,15 @@ public final class RocksStorage {
final BackupEngine backupEngine = BackupEngine.open(RocksStorage.this.options.getEnv(), backupOpt);
final RestoreOptions options = new RestoreOptions(true);
final DBOptions dbOptions = RocksStorage.this.options;
backupEngine
.restoreDbFromBackup(info.getBackupId(), dbPath, dbOptions.walDir(),
options);
backupEngine.restoreDbFromBackup(info.getBackupId(), dbPath, dbOptions.walDir(), options);
} catch (RocksDBException ex) {
Status status = ex.getStatus();
throw createRocksStorageException(status);
throw createRocksStorageException(ErrorCode.RocksDBSnapshotLoadError, status);
} catch (Throwable ex) {
throw new RocksStorageException(NacosException.SERVER_ERROR, ex);
throw new RocksStorageException(ErrorCode.UnKnowError.getCode(), ex);
}
}
public void shutdown() {
this.defaultHandle.close();
this.db.close();
@ -251,13 +250,13 @@ public final class RocksStorage {
this.writeOptions.close();
this.readOptions.close();
}
private static RocksStorageException createRocksStorageException(Status status) {
private static RocksStorageException createRocksStorageException(ErrorCode code, Status status) {
RocksStorageException exception = new RocksStorageException();
exception.setErrCode(status.getCode().getValue());
exception.setErrCode(code.getCode());
exception.setErrMsg(String.format("RocksDB error msg : code=%s, subCode=%s, state=%s", status.getCode(),
status.getSubCode(), status.getState()));
return exception;
}
}

View File

@ -32,6 +32,7 @@ import com.alibaba.nacos.consistency.entity.Log;
import com.alibaba.nacos.consistency.entity.Response;
import com.alibaba.nacos.consistency.snapshot.SnapshotOperation;
import com.alibaba.nacos.core.distributed.raft.RaftConfig;
import com.alibaba.nacos.core.exception.ErrorCode;
import com.alibaba.nacos.core.exception.RocksStorageException;
import com.alibaba.nacos.core.storage.RocksStorage;
import com.alibaba.nacos.naming.consistency.Datum;
@ -109,7 +110,7 @@ public class PersistentServiceProcessor extends LogProcessor4CP implements Persi
byte[] data = rocksStorage.get(ByteUtils.toBytes(key));
return serializer.deserialize(data);
} catch (RocksStorageException ex) {
throw new NacosRuntimeException(NacosException.SERVER_ERROR, ex.getErrMsg());
throw new NacosRuntimeException(ex.getErrCode(), ex.getErrMsg());
}
});
init();
@ -229,7 +230,7 @@ public class PersistentServiceProcessor extends LogProcessor4CP implements Persi
try {
protocol.submit(log);
} catch (Exception e) {
throw new NacosException(NacosException.SERVER_ERROR, e.getMessage());
throw new NacosException(ErrorCode.ProtoSubmitError.getCode(), e.getMessage());
}
}
@ -242,7 +243,7 @@ public class PersistentServiceProcessor extends LogProcessor4CP implements Persi
try {
protocol.submit(log);
} catch (Exception e) {
throw new NacosException(NacosException.SERVER_ERROR, e.getMessage());
throw new NacosException(ErrorCode.ProtoSubmitError.getCode(), e.getMessage());
}
}
@ -257,9 +258,9 @@ public class PersistentServiceProcessor extends LogProcessor4CP implements Persi
if (resp.getSuccess()) {
return serializer.deserialize(resp.getData().toByteArray(), Datum.class);
}
throw new NacosException(NacosException.SERVER_ERROR, resp.getErrMsg());
throw new NacosException(ErrorCode.ProtoReadError.getCode(), resp.getErrMsg());
} catch (Exception e) {
throw new NacosException(NacosException.SERVER_ERROR, e.getMessage());
throw new NacosException(ErrorCode.ProtoReadError.getCode(), e.getMessage());
}
}

View File

@ -18,6 +18,8 @@
package com.alibaba.nacos.naming.utils;
/**
* Naming module code starts with 20001.
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
public final class Constants {