Skip to content

Commit

Permalink
opt: support error result (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
funky-eyes authored Jan 13, 2025
1 parent 1b9f9fa commit 38682b8
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.alipay.remoting.RemotingContext;
import com.alipay.remoting.RemotingProcessor;
import icu.funkye.redispike.handler.process.impl.GetRequestProcessor;
import icu.funkye.redispike.handler.process.impl.NotSupportProcessor;
import icu.funkye.redispike.handler.process.impl.hash.HDelRequestProcessor;
import icu.funkye.redispike.handler.process.impl.hash.HExistsRequestProcessor;
import icu.funkye.redispike.handler.process.impl.hash.HGetAllRequestProcessor;
Expand Down Expand Up @@ -103,6 +104,8 @@ public RedisCommandHandler() {
processorMap.put(hLenRequestProcessor.getCmdCode().value(), hLenRequestProcessor);
HKeysRequestProcessor hKeysRequestProcessor = new HKeysRequestProcessor();
processorMap.put(hKeysRequestProcessor.getCmdCode().value(), hKeysRequestProcessor);
NotSupportProcessor notSupportProcessor = new NotSupportProcessor();
processorMap.put(notSupportProcessor.getCmdCode().value(), notSupportProcessor);
}

@Override
Expand All @@ -119,7 +122,7 @@ public void handleCommand(RemotingContext ctx, Object msg) {

private void processSingleRequest(RemotingContext ctx, Object object) {
if (object instanceof RedisRequest) {
RedisRequest request = (RedisRequest) object;
RedisRequest<?> request = (RedisRequest<?>) object;
try {
processorMap.get(request.getCmdCode().value()).process(ctx, request, getDefaultExecutor());
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 icu.funkye.redispike.handler.process.impl;

import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import com.alipay.remoting.RemotingContext;
import icu.funkye.redispike.handler.process.AbstractRedisRequestProcessor;
import icu.funkye.redispike.protocol.RedisRequestCommandCode;
import icu.funkye.redispike.protocol.request.NotSupportRequest;
import icu.funkye.redispike.util.IntegerUtils;

/**
* @author [email protected]
*/
public class NotSupportProcessor extends AbstractRedisRequestProcessor<NotSupportRequest> {

public NotSupportProcessor() {
this.cmdCode = new RedisRequestCommandCode(IntegerUtils.hashCodeToShort(NotSupportRequest.class.hashCode()));
}

@Override
public void handle(RemotingContext ctx, NotSupportRequest request) {
request.setResponse("ERR unknown command `"+request.getCommand()+"`, with args beginning with:");
Optional.ofNullable(request.getCountDownLatch()).ifPresent(CountDownLatch::countDown);
write(ctx, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import com.alipay.remoting.CommandDecoder;
import icu.funkye.redispike.protocol.request.NotSupportRequest;
import icu.funkye.redispike.protocol.request.hash.HDelRequest;
import icu.funkye.redispike.protocol.request.hash.HExistsRequest;
import icu.funkye.redispike.protocol.request.hash.HGetAllRequest;
Expand Down Expand Up @@ -152,7 +153,7 @@ private AbstractRedisRequest<?> convert2RedisRequest(List<String> params, boolea
return new SPopRequest(params.remove(0), params.size() > 0 ? Integer.parseInt(params.get(0)) : null,
flush);
default:
return null;
return new NotSupportRequest(params.get(0), flush);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 icu.funkye.redispike.protocol.request;

import icu.funkye.redispike.protocol.AbstractRedisRequest;
import icu.funkye.redispike.protocol.RedisResponse;
import icu.funkye.redispike.protocol.response.BulkResponse;

public class NotSupportRequest extends AbstractRedisRequest<String> {

String command;

BulkResponse response = new BulkResponse();

public NotSupportRequest(String command, boolean flush) {
this.flush = flush;
this.command = command;
}

public String getCommand() {
return command;
}

@Override
public void setResponse(String data) {
this.response.setError(data);
}

@Override
public RedisResponse<String> getResponse() {
return response;
}

@Override
public String toString() {
return "GetRequest{" + "key='" + command + '\'' + ", response=" + response + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package icu.funkye.redispike.protocol.response;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import com.alipay.remoting.util.StringUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static ExecutorService newVirtualThreadPerTaskExecutor() {
LOGGER.error(e.getMessage(), e);
}
}
return new ThreadPoolExecutor(0, 200, 120L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(),
return new ThreadPoolExecutor(0, 200, 120L, TimeUnit.SECONDS, new SynchronousQueue<>(),
new ThreadDaemonFactory());
}

Expand Down
15 changes: 13 additions & 2 deletions src/test/java/icu/funkye/redispike/ServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.params.SetParams;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
Expand All @@ -62,6 +63,16 @@ public static void init() throws ParseException {
aspClient = AeroSpikeClientFactory.getClient();
}

@Test
@Order(value = Integer.MIN_VALUE)
public void TestErr() {
try (Jedis jedis = JedisPooledFactory.getJedisInstance()) {
jedis.aclDelUser("test");
} catch (Exception e) {
Assertions.assertInstanceOf(JedisDataException.class, e);
}
}

@Test
@Order(value = Integer.MIN_VALUE)
public void TestPippline() {
Expand Down Expand Up @@ -136,7 +147,7 @@ public void TestSet() {
@Test
@DisabledIfSystemProperty(named = "asp-client.version", matches = "4.1.2")
public void testKeys() {
/* List<String> keys = new ArrayList<>();
List<String> keys = new ArrayList<>();
for (int i = 0; i < 2; i++) {
keys.add(String.valueOf(ThreadLocalRandom.current().nextLong(RandomValue)));
}
Expand All @@ -153,7 +164,7 @@ public void testKeys() {
Assertions.assertEquals(result.size(), 1);
result = jedis.keys("*123");
Assertions.assertEquals(result.size(), 1);
}*/
}
}

@Test
Expand Down

0 comments on commit 38682b8

Please sign in to comment.