@Override
    public Response onApply(WriteRequest request) {
        readLock.lock();
        try {
            MetadataOperation<ServiceMetadata> op = serializer.deserialize(request.getData().toByteArray(), processType);
            switch (DataOperation.valueOf(request.getOperation())) {
                case ADD:
                    addClusterMetadataToService(op);
                    break;
                case CHANGE:
                    updateServiceMetadata(op);
                    break;
                case DELETE:
                    deleteServiceMetadata(op);
                    break;
                default:
                    return Response.newBuilder().setSuccess(false)
                            .setErrMsg("Unsupported operation " + request.getOperation()).build();
            }
            return Response.newBuilder().setSuccess(true).build();
        } catch (Exception e) {
            Loggers.RAFT.error("onApply {} service metadata operation failed. ", request.getOperation(), e);
            String errorMessage = null == e.getMessage() ? e.getClass().getName() : e.getMessage();
            return Response.newBuilder().setSuccess(false).setErrMsg(errorMessage).build();
        } finally {
            readLock.unlock();
        }
    }
@Test
    void testOnApply() throws NoSuchFieldException, IllegalAccessException {
        WriteRequest defaultInstance = WriteRequest.getDefaultInstance();
        Class<WriteRequest> writeRequestClass = WriteRequest.class;
        Field operation = writeRequestClass.getDeclaredField("operation_");
        operation.setAccessible(true);
        operation.set(defaultInstance, "ADD");
        
        MetadataOperation<ServiceMetadata> metadataOperation = new MetadataOperation<>();
        ServiceMetadata serviceMetadata = new ServiceMetadata();
        metadataOperation.setMetadata(serviceMetadata);
        metadataOperation.setServiceName("nacos");
        metadataOperation.setNamespace("namespace");
        metadataOperation.setGroup("group");
        Serializer aDefault = SerializeFactory.getDefault();
        ByteString bytes = ByteString.copyFrom(aDefault.serialize(metadataOperation));
        
        Field data = writeRequestClass.getDeclaredField("data_");
        data.setAccessible(true);
        data.set(defaultInstance, bytes);
        
        // ADD
        Response addResponse = serviceMetadataProcessor.onApply(defaultInstance);
        
        Service service = Service.newService(metadataOperation.getNamespace(), metadataOperation.getGroup(),
                metadataOperation.getServiceName(), metadataOperation.getMetadata().isEphemeral());
        Service singleton = ServiceManager.getInstance().getSingleton(service);
        namingMetadataManager.updateServiceMetadata(singleton, metadataOperation.getMetadata());
        
        assertTrue(addResponse.getSuccess());
        verify(namingMetadataManager).getServiceMetadata(service);
        verify(namingMetadataManager).updateServiceMetadata(service, serviceMetadata);
        
        // CHANGE
        operation.set(defaultInstance, "CHANGE");
        Response changeResponse = serviceMetadataProcessor.onApply(defaultInstance);
        
        assertTrue(changeResponse.getSuccess());
        verify(namingMetadataManager, times(2)).getServiceMetadata(service);
        verify(namingMetadataManager).updateServiceMetadata(service, serviceMetadata);
        
        // DELETE
        operation.set(defaultInstance, "DELETE");
        Response deleteResponse = serviceMetadataProcessor.onApply(defaultInstance);
        
        assertTrue(deleteResponse.getSuccess());
        verify(namingMetadataManager).removeServiceMetadata(service);
        verify(serviceStorage).removeData(service);
        
        // VERIFY
        operation.set(defaultInstance, "VERIFY");
        Response otherResponse = serviceMetadataProcessor.onApply(defaultInstance);
        
        assertFalse(otherResponse.getSuccess());
    }