You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2018/12/07 09:11:49 UTC
[GitHub] ngjaying opened a new issue #3140: Function state does not work
ngjaying opened a new issue #3140: Function state does not work
URL: https://github.com/apache/pulsar/issues/3140
#### Expected behavior
Function context getState can read the state set by putState
#### Actual behavior
Function context getState does not return the state set by putState
#### Steps to reproduce
Run the following function on the cluster. The incrCounter/getCounter works fine; but the getState always return empty string.
```
import static java.nio.charset.StandardCharsets.UTF_8;
import java.nio.ByteBuffer;
import org.apache.pulsar.functions.api.Context;
import org.apache.pulsar.functions.api.Function;
import org.slf4j.Logger;
public class StateFunction implements Function<String, Void> {
private Logger logger;
@Override
public Void process(String input, Context context) throws Exception {
logger = context.getLogger();
logger.info("received {}", input);
context.incrCounter("ac", 1);
long currentCount = context.getCounter("ac");
logger.info("current count: {}", currentCount);
try {
ByteBuffer rulesState = context.getState("last");
if(rulesState != null) {
logger.info("last: {}", UTF_8.decode(rulesState).toString());
}
}catch(Exception e) {
e.printStackTrace();
}
context.putState("last", ByteBuffer.wrap(input.getBytes(UTF_8)));
try {
ByteBuffer rulesState = context.getState("last");
if(rulesState != null) {
logger.info("new last: {}", UTF_8.decode(rulesState).toString());
}
}catch(Exception e) {
e.printStackTrace();
}
return null;
}
}
```
#### System configuration
**Pulsar version**: 2.2.0
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services