Search in sources :

Example 16 with WriteRequest

use of com.alibaba.nacos.consistency.entity.WriteRequest in project nacos by alibaba.

the class PersistentClientOperationServiceImpl method registerInstance.

@Override
public void registerInstance(Service service, Instance instance, String clientId) {
    Service singleton = ServiceManager.getInstance().getSingleton(service);
    if (singleton.isEphemeral()) {
        throw new NacosRuntimeException(NacosException.INVALID_PARAM, String.format("Current service %s is ephemeral service, can't register persistent instance.", singleton.getGroupedServiceName()));
    }
    final InstanceStoreRequest request = new InstanceStoreRequest();
    request.setService(service);
    request.setInstance(instance);
    request.setClientId(clientId);
    final WriteRequest writeRequest = WriteRequest.newBuilder().setGroup(group()).setData(ByteString.copyFrom(serializer.serialize(request))).setOperation(DataOperation.ADD.name()).build();
    try {
        protocol.write(writeRequest);
    } catch (Exception e) {
        throw new NacosRuntimeException(NacosException.SERVER_ERROR, e);
    }
}
Also used : WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) ClientOperationService(com.alibaba.nacos.naming.core.v2.service.ClientOperationService) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException) NacosException(com.alibaba.nacos.api.exception.NacosException) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException) IOException(java.io.IOException)

Example 17 with WriteRequest

use of com.alibaba.nacos.consistency.entity.WriteRequest in project nacos by alibaba.

the class NamingMetadataOperateService method addClusterMetadata.

/**
 * Add cluster metadata to service metadata.
 *
 * @param service         service
 * @param clusterName     cluster name
 * @param clusterMetadata cluster metadata
 */
public void addClusterMetadata(Service service, String clusterName, ClusterMetadata clusterMetadata) {
    MetadataOperation<ServiceMetadata> operation = buildMetadataOperation(service);
    ServiceMetadata serviceMetadata = new ServiceMetadata();
    serviceMetadata.setEphemeral(service.isEphemeral());
    serviceMetadata.getClusters().put(clusterName, clusterMetadata);
    operation.setMetadata(serviceMetadata);
    WriteRequest operationLog = WriteRequest.newBuilder().setGroup(Constants.SERVICE_METADATA).setOperation(DataOperation.ADD.name()).setData(ByteString.copyFrom(serializer.serialize(operation))).build();
    submitMetadataOperation(operationLog);
}
Also used : WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest)

Example 18 with WriteRequest

use of com.alibaba.nacos.consistency.entity.WriteRequest in project nacos by alibaba.

the class NamingMetadataOperateService method deleteServiceMetadata.

/**
 * Delete service metadata.
 *
 * @param service service of metadata
 */
public void deleteServiceMetadata(Service service) {
    MetadataOperation<ServiceMetadata> operation = buildMetadataOperation(service);
    WriteRequest operationLog = WriteRequest.newBuilder().setGroup(Constants.SERVICE_METADATA).setOperation(DataOperation.DELETE.name()).setData(ByteString.copyFrom(serializer.serialize(operation))).build();
    submitMetadataOperation(operationLog);
}
Also used : WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest)

Example 19 with WriteRequest

use of com.alibaba.nacos.consistency.entity.WriteRequest in project nacos by alibaba.

the class ProtoMessageUtilTest method testProto.

@Test
public void testProto() throws Exception {
    WriteRequest request = WriteRequest.newBuilder().setKey("test-proto-new").build();
    byte[] bytes = request.toByteArray();
    Log log = Log.parseFrom(bytes);
    assertEquals(request.getKey(), log.getKey());
}
Also used : Log(com.alibaba.nacos.consistency.entity.Log) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) Test(org.junit.Test)

Example 20 with WriteRequest

use of com.alibaba.nacos.consistency.entity.WriteRequest in project nacos by alibaba.

the class ProtoMessageUtilTest method testConvertToWriteRequest.

@Test
public void testConvertToWriteRequest() {
    ByteString data = ByteString.copyFrom("data".getBytes());
    Log log = Log.newBuilder().setKey("key").setGroup("group").setData(data).setOperation("o").putExtendInfo("k", "v").build();
    WriteRequest writeRequest = ProtoMessageUtil.convertToWriteRequest(log);
    assertEquals(1, writeRequest.getExtendInfoCount());
    assertEquals(data, writeRequest.getData());
    assertEquals("key", writeRequest.getKey());
    assertEquals("group", writeRequest.getGroup());
    assertEquals("o", writeRequest.getOperation());
}
Also used : Log(com.alibaba.nacos.consistency.entity.Log) ByteString(com.google.protobuf.ByteString) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) Test(org.junit.Test)

Aggregations

WriteRequest (com.alibaba.nacos.consistency.entity.WriteRequest)21 NacosException (com.alibaba.nacos.api.exception.NacosException)7 Test (org.junit.Test)7 Response (com.alibaba.nacos.consistency.entity.Response)5 ByteString (com.google.protobuf.ByteString)5 NacosRuntimeException (com.alibaba.nacos.api.exception.runtime.NacosRuntimeException)3 RequestProcessor4CP (com.alibaba.nacos.consistency.cp.RequestProcessor4CP)3 ReadRequest (com.alibaba.nacos.consistency.entity.ReadRequest)3 Field (java.lang.reflect.Field)3 NotifyCenter (com.alibaba.nacos.common.notify.NotifyCenter)2 ExceptionUtil (com.alibaba.nacos.common.utils.ExceptionUtil)2 JacksonUtils (com.alibaba.nacos.common.utils.JacksonUtils)2 LoggerUtils (com.alibaba.nacos.common.utils.LoggerUtils)2 Serializer (com.alibaba.nacos.consistency.Serializer)2 Log (com.alibaba.nacos.consistency.entity.Log)2 ConsistencyException (com.alibaba.nacos.consistency.exception.ConsistencyException)2 SnapshotOperation (com.alibaba.nacos.consistency.snapshot.SnapshotOperation)2 Datum (com.alibaba.nacos.naming.consistency.Datum)2 Service (com.alibaba.nacos.naming.core.v2.pojo.Service)2 ByteBuffer (java.nio.ByteBuffer)2