use of voldemort.utils.ByteArray in project voldemort by voldemort.
the class AdminServiceRequestHandler method handleGetMetadata.
public VAdminProto.GetMetadataResponse handleGetMetadata(VAdminProto.GetMetadataRequest request) {
VAdminProto.GetMetadataResponse.Builder response = VAdminProto.GetMetadataResponse.newBuilder();
try {
ByteArray key = ProtoUtils.decodeBytes(request.getKey());
String keyString = ByteUtils.getString(key.get(), "UTF-8");
if (keyString.isEmpty()) {
// a new instance, with an empty key field.
throw new VoldemortException("Received admin operation which got interpreted as a " + "GetMetadataRequest with an empty metadata key. This " + "typically means that the requested admin operation is not " + "supported on this version of the Voldemort server.");
}
List<Versioned<byte[]>> versionedList = metadataStore.get(key, null);
if (versionedList.size() > 0) {
Versioned<byte[]> versioned = versionedList.get(0);
response.setVersion(ProtoUtils.encodeVersioned(versioned));
}
} catch (VoldemortException e) {
response.setError(ProtoUtils.encodeError(errorCodeMapper, e));
String errorMessage = "handleGetMetadata failed for request(" + request.toString() + ")";
if (e instanceof StoreNotFoundException) {
logger.info(errorMessage + " with " + StoreNotFoundException.class.getSimpleName());
} else {
logger.error(errorMessage, e);
}
}
return response.build();
}
use of voldemort.utils.ByteArray in project voldemort by voldemort.
the class AdminServiceRequestHandler method handleUpdateMetadataPair.
public VAdminProto.UpdateMetadataPairResponse handleUpdateMetadataPair(VAdminProto.UpdateMetadataPairRequest request) {
VAdminProto.UpdateMetadataPairResponse.Builder response = VAdminProto.UpdateMetadataPairResponse.newBuilder();
try {
ByteArray clusterKey = ProtoUtils.decodeBytes(request.getClusterKey());
ByteArray storesKey = ProtoUtils.decodeBytes(request.getStoresKey());
String clusterKeyString = ByteUtils.getString(clusterKey.get(), "UTF-8");
String storesKeyString = ByteUtils.getString(storesKey.get(), "UTF-8");
if (MetadataStore.METADATA_KEYS.contains(clusterKeyString) && MetadataStore.METADATA_KEYS.contains(storesKeyString)) {
Versioned<byte[]> clusterVersionedValue = ProtoUtils.decodeVersioned(request.getClusterValue());
Versioned<byte[]> storesVersionedValue = ProtoUtils.decodeVersioned(request.getStoresValue());
metadataStore.writeLock.lock();
try {
logger.info("Updating metadata for keys '" + clusterKeyString + "'" + " and '" + storesKeyString + "'");
metadataStore.put(clusterKey, clusterVersionedValue, null);
// replace this with put
metadataStore.put(storesKey, storesVersionedValue, null);
// metadataStore.updateStoreDefinitions(storesVersionedValue);
logger.info("Successfully updated metadata for keys '" + clusterKeyString + "'" + " and '" + storesKeyString + "'");
} finally {
metadataStore.writeLock.unlock();
}
}
} catch (VoldemortException e) {
response.setError(ProtoUtils.encodeError(errorCodeMapper, e));
logger.error("handleUpdateMetadataPair failed for request(" + request.toString() + ")", e);
}
return response.build();
}
use of voldemort.utils.ByteArray in project voldemort by voldemort.
the class AdminServiceRequestHandler method handleUpdateStoreDefinitions.
public VAdminProto.UpdateMetadataResponse handleUpdateStoreDefinitions(VAdminProto.UpdateMetadataRequest request) {
VAdminProto.UpdateMetadataResponse.Builder response = VAdminProto.UpdateMetadataResponse.newBuilder();
try {
ByteArray key = ProtoUtils.decodeBytes(request.getKey());
String keyString = ByteUtils.getString(key.get(), "UTF-8");
if (MetadataStore.METADATA_KEYS.contains(keyString)) {
Versioned<byte[]> versionedValue = ProtoUtils.decodeVersioned(request.getVersioned());
// corresponding put
if (keyString.equals(MetadataStore.STORES_KEY)) {
metadataStore.updateStoreDefinitions(versionedValue);
}
logger.info("Successfully updated metadata for key '" + keyString + "'");
}
} catch (VoldemortException e) {
response.setError(ProtoUtils.encodeError(errorCodeMapper, e));
logger.error("handleUpdateMetadata failed for request(" + request.toString() + ")", e);
}
return response.build();
}
use of voldemort.utils.ByteArray in project voldemort by voldemort.
the class AdminServiceRequestHandler method handleAddStore.
public VAdminProto.AddStoreResponse handleAddStore(VAdminProto.AddStoreRequest request) {
VAdminProto.AddStoreResponse.Builder response = VAdminProto.AddStoreResponse.newBuilder();
// don't try to add a store when not in normal or offline state
if (!metadataStore.getServerStateUnlocked().equals(MetadataStore.VoldemortState.NORMAL_SERVER) && !metadataStore.getServerStateUnlocked().equals(MetadataStore.VoldemortState.OFFLINE_SERVER)) {
response.setError(ProtoUtils.encodeError(errorCodeMapper, new VoldemortException("Voldemort server is neither in normal state nor in offline state")));
return response.build();
}
AdminClient adminClient = null;
try {
// adding a store requires decoding the passed in store string
StoreDefinitionsMapper mapper = new StoreDefinitionsMapper();
StoreDefinition def = mapper.readStore(new StringReader(request.getStoreDefinition()));
adminClient = new AdminClient(metadataStore.getCluster());
synchronized (lock) {
if (!storeRepository.hasLocalStore(def.getName())) {
if (def.getReplicationFactor() > metadataStore.getCluster().getNumberOfNodes()) {
throw new StoreOperationFailureException("Cannot add a store whose replication factor ( " + def.getReplicationFactor() + " ) is greater than the number of nodes ( " + metadataStore.getCluster().getNumberOfNodes() + " )");
}
logger.info("Adding new store '" + def.getName() + "'");
// open the store
StorageEngine<ByteArray, byte[], byte[]> engine = storageService.openStore(def);
// effect of updating the stores.xml file)
try {
metadataStore.addStoreDefinition(def);
long defaultQuota = voldemortConfig.getDefaultStorageSpaceQuotaInKB();
QuotaUtils.setQuota(def.getName(), QuotaType.STORAGE_SPACE, storeRepository, metadataStore.getCluster().getNodeIds(), defaultQuota);
} catch (Exception e) {
// rollback open store operation
boolean isReadOnly = ReadOnlyStorageConfiguration.TYPE_NAME.equals(def.getType());
storageService.removeEngine(engine, isReadOnly, def.getType(), true);
throw new VoldemortException(e);
}
logger.info("Successfully added new store '" + def.getName() + "'");
} else {
logger.error("Failure to add a store with the same name '" + def.getName() + "'");
throw new StoreOperationFailureException(String.format("Store '%s' already exists on this server", def.getName()));
}
}
} catch (VoldemortException e) {
response.setError(ProtoUtils.encodeError(errorCodeMapper, e));
logger.error("handleAddStore failed for request(" + request.toString() + ")", e);
} finally {
if (adminClient != null) {
IOUtils.closeQuietly(adminClient);
}
}
return response.build();
}
use of voldemort.utils.ByteArray in project voldemort by voldemort.
the class AdminServiceRequestHandler method handleSetMetadata.
public VAdminProto.UpdateMetadataResponse handleSetMetadata(VAdminProto.UpdateMetadataRequest request) {
VAdminProto.UpdateMetadataResponse.Builder response = VAdminProto.UpdateMetadataResponse.newBuilder();
try {
ByteArray keyBytes = ProtoUtils.decodeBytes(request.getKey());
String keyString = ByteUtils.getString(keyBytes.get(), "UTF-8");
if (MetadataStore.METADATA_KEYS.contains(keyString)) {
Versioned<byte[]> versionedValue = ProtoUtils.decodeVersioned(request.getVersioned());
logger.info("Updating metadata for key '" + keyString + "'");
metadataStore.validate(keyBytes, versionedValue, null);
metadataStore.put(keyBytes, versionedValue, null);
if (MetadataStore.CLUSTER_KEY.equals(keyString)) {
server.handleClusterUpdate();
} else if (MetadataStore.NODE_ID_KEY.endsWith(keyString)) {
server.refreshNodeIdFromMetadata();
}
logger.info("Successfully updated metadata for key '" + keyString + "'");
}
} catch (VoldemortException e) {
response.setError(ProtoUtils.encodeError(errorCodeMapper, e));
logger.error("handleUpdateMetadata failed for request(" + request.toString() + ")", e);
}
return response.build();
}
Aggregations