use of org.apache.ignite.internal.processors.cache.CacheConfigurationOverride in project ignite by apache.
the class GridCacheCommandHandler method handleAsync.
/**
* {@inheritDoc}
*/
@Override
public IgniteInternalFuture<GridRestResponse> handleAsync(final GridRestRequest req) {
assert req instanceof GridRestCacheRequest : "Invalid command for topology handler: " + req;
assert SUPPORTED_COMMANDS.contains(req.command());
if (log.isDebugEnabled())
log.debug("Handling cache REST request: " + req);
GridRestCacheRequest req0 = (GridRestCacheRequest) req;
final String cacheName = req0.cacheName() == null ? DFLT_CACHE_NAME : req0.cacheName();
final Object key = req0.key();
final Set<GridClientCacheFlag> cacheFlags = GridClientCacheFlag.parseCacheFlags(req0.cacheFlags());
try {
GridRestCommand cmd = req0.command();
if (key == null && KEY_REQUIRED_REQUESTS.contains(cmd))
throw new IgniteCheckedException(GridRestCommandHandlerAdapter.missingParameter("key"));
final Long ttl = req0.ttl();
IgniteInternalFuture<GridRestResponse> fut;
switch(cmd) {
case DESTROY_CACHE:
{
// Do not check thread tx here since there can be active system cache txs.
fut = ((IgniteKernal) ctx.grid()).destroyCacheAsync(cacheName, false, false).chain(new CX1<IgniteInternalFuture<?>, GridRestResponse>() {
@Override
public GridRestResponse applyx(IgniteInternalFuture<?> f) throws IgniteCheckedException {
f.get();
return new GridRestResponse(null);
}
});
break;
}
case GET_OR_CREATE_CACHE:
{
String templateName = req0.templateName();
if (F.isEmpty(templateName))
templateName = TEMPLATE_PARTITIONED;
CacheConfigurationOverride cfgOverride = req0.configuration();
boolean dfltPartTemplate = F.isEmpty(templateName) || TEMPLATE_PARTITIONED.equalsIgnoreCase(templateName);
boolean dfltReplTemplate = TEMPLATE_REPLICATED.equalsIgnoreCase(templateName);
if (dfltPartTemplate || dfltReplTemplate) {
if (cfgOverride == null)
cfgOverride = new CacheConfigurationOverride();
cfgOverride.mode(dfltPartTemplate ? PARTITIONED : REPLICATED);
if (cfgOverride.writeSynchronizationMode() == null)
cfgOverride.writeSynchronizationMode(FULL_SYNC);
}
// Do not check thread tx here since there can be active system cache txs.
fut = ((IgniteKernal) ctx.grid()).getOrCreateCacheAsync(cacheName, templateName, cfgOverride, false).chain(new CX1<IgniteInternalFuture<?>, GridRestResponse>() {
@Override
public GridRestResponse applyx(IgniteInternalFuture<?> f) throws IgniteCheckedException {
f.get();
return new GridRestResponse(null);
}
});
break;
}
case CACHE_METADATA:
{
fut = ctx.task().execute(MetadataTask.class, req0.cacheName());
break;
}
case CACHE_CONTAINS_KEYS:
{
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new ContainsKeysCommand(getKeys(req0)));
break;
}
case CACHE_CONTAINS_KEY:
{
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new ContainsKeyCommand(key));
break;
}
case CACHE_GET:
{
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new GetCommand(key));
break;
}
case CACHE_GET_AND_PUT:
{
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new GetAndPutCommand(key, getValue(req0)));
break;
}
case CACHE_GET_AND_REPLACE:
{
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new GetAndReplaceCommand(key, getValue(req0)));
break;
}
case CACHE_GET_AND_PUT_IF_ABSENT:
{
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new GetAndPutIfAbsentCommand(key, getValue(req0)));
break;
}
case CACHE_PUT_IF_ABSENT:
{
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new PutIfAbsentCommand(key, ttl, getValue(req0)));
break;
}
case CACHE_GET_ALL:
{
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new GetAllCommand(getKeys(req0)));
break;
}
case CACHE_PUT:
{
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new PutCommand(key, ttl, getValue(req0)));
break;
}
case CACHE_ADD:
{
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new AddCommand(key, ttl, getValue(req0)));
break;
}
case CACHE_PUT_ALL:
{
Map<Object, Object> map = req0.values();
if (F.isEmpty(map))
throw new IgniteCheckedException(GridRestCommandHandlerAdapter.missingParameter("values"));
for (Map.Entry<Object, Object> e : map.entrySet()) {
if (e.getKey() == null)
throw new IgniteCheckedException("Failing putAll operation (null keys are not allowed).");
if (e.getValue() == null)
throw new IgniteCheckedException("Failing putAll operation (null values are not allowed).");
}
// HashMap wrapping for correct serialization
map = new HashMap<>(map);
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new PutAllCommand(map));
break;
}
case CACHE_REMOVE:
{
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new RemoveCommand(key));
break;
}
case CACHE_REMOVE_VALUE:
{
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new RemoveValueCommand(key, getValue(req0)));
break;
}
case CACHE_REPLACE_VALUE:
{
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new ReplaceValueCommand(key, getValue(req0), req0.value2()));
break;
}
case CACHE_GET_AND_REMOVE:
{
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new GetAndRemoveCommand(key));
break;
}
case CACHE_REMOVE_ALL:
{
Map<Object, Object> map = req0.values();
// HashSet wrapping for correct serialization
Set<Object> keys = map == null ? null : new HashSet<>(map.keySet());
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new RemoveAllCommand(keys));
break;
}
case CACHE_CLEAR:
{
Map<Object, Object> map = req0.values();
// HashSet wrapping for correct serialization
Set<Object> cacheNames = map == null ? new HashSet<Object>(ctx.cache().publicCacheNames()) : new HashSet<>(map.keySet());
GridCompoundFuture compFut = new GridCompoundFuture();
for (Object cName : cacheNames) compFut.add(executeCommand(req.destinationId(), req.clientId(), (String) cName, cacheFlags, key, new RemoveAllCommand(null)));
compFut.markInitialized();
fut = compFut.chain(new CX1<GridCompoundFuture<GridCacheRestResponse, ?>, GridRestResponse>() {
@Override
public GridRestResponse applyx(GridCompoundFuture<GridCacheRestResponse, ?> cf) throws IgniteCheckedException {
boolean success = true;
for (IgniteInternalFuture<GridCacheRestResponse> f : cf.futures()) if ((Boolean) f.get().getResponse() != true)
success = false;
GridCacheRestResponse resp = new GridCacheRestResponse();
if (success)
resp.setResponse(true);
else
resp.setResponse(false);
return resp;
}
});
break;
}
case CACHE_REPLACE:
{
final Object val = req0.value();
if (val == null)
throw new IgniteCheckedException(GridRestCommandHandlerAdapter.missingParameter("val"));
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new ReplaceCommand(key, ttl, val));
break;
}
case CACHE_CAS:
{
final Object val1 = req0.value();
final Object val2 = req0.value2();
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new CasCommand(val2, val1, key));
break;
}
case CACHE_APPEND:
{
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new AppendCommand(key, req0));
break;
}
case CACHE_PREPEND:
{
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, cacheFlags, key, new PrependCommand(key, req0));
break;
}
case CACHE_METRICS:
{
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, key, new MetricsCommand());
break;
}
case CACHE_SIZE:
{
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, key, new SizeCommand());
break;
}
case CACHE_UPDATE_TLL:
{
if (ttl == null)
throw new IgniteCheckedException(GridRestCommandHandlerAdapter.missingParameter("ttl"));
fut = executeCommand(req.destinationId(), req.clientId(), cacheName, key, new UpdateTllCommand(key, ttl));
break;
}
default:
throw new IllegalArgumentException("Invalid command for cache handler: " + req);
}
return fut;
} catch (IgniteException | IgniteCheckedException e) {
U.error(log, "Failed to execute cache command: " + req, e);
return new GridFinishedFuture<>(e);
} finally {
if (log.isDebugEnabled())
log.debug("Handled cache REST request: " + req);
}
}
use of org.apache.ignite.internal.processors.cache.CacheConfigurationOverride in project ignite by apache.
the class GridJettyRestHandler method createRequest.
/**
* Creates REST request.
*
* @param cmd Command.
* @param params Parameters.
* @param req Servlet request.
* @return REST request.
* @throws IgniteCheckedException If creation failed.
*/
@Nullable
private GridRestRequest createRequest(GridRestCommand cmd, Map<String, Object> params, HttpServletRequest req) throws IgniteCheckedException {
GridRestRequest restReq;
switch(cmd) {
case GET_OR_CREATE_CACHE:
{
GridRestCacheRequest restReq0 = new GridRestCacheRequest();
restReq0.cacheName((String) params.get(CACHE_NAME_PARAM));
String templateName = (String) params.get(TEMPLATE_NAME_PARAM);
if (!F.isEmpty(templateName))
restReq0.templateName(templateName);
String backups = (String) params.get(BACKUPS_PARAM);
CacheConfigurationOverride cfg = new CacheConfigurationOverride();
// Set cache backups.
if (!F.isEmpty(backups)) {
try {
cfg.backups(Integer.parseInt(backups));
} catch (NumberFormatException e) {
throw new IgniteCheckedException("Failed to parse number of cache backups: " + backups, e);
}
}
// Set cache group name.
String cacheGrp = (String) params.get(CACHE_GROUP_PARAM);
if (!F.isEmpty(cacheGrp))
cfg.cacheGroup(cacheGrp);
// Set cache data region name.
String dataRegion = (String) params.get(DATA_REGION_PARAM);
if (!F.isEmpty(dataRegion))
cfg.dataRegion(dataRegion);
// Set cache write mode.
String wrtSyncMode = (String) params.get(WRITE_SYNCHRONIZATION_MODE_PARAM);
if (!F.isEmpty(wrtSyncMode)) {
try {
cfg.writeSynchronizationMode(CacheWriteSynchronizationMode.valueOf(wrtSyncMode));
} catch (IllegalArgumentException e) {
throw new IgniteCheckedException("Failed to parse cache write synchronization mode: " + wrtSyncMode, e);
}
}
if (!cfg.isEmpty())
restReq0.configuration(cfg);
restReq = restReq0;
break;
}
case DESTROY_CACHE:
{
GridRestCacheRequest restReq0 = new GridRestCacheRequest();
restReq0.cacheName((String) params.get(CACHE_NAME_PARAM));
restReq = restReq0;
break;
}
case ATOMIC_DECREMENT:
case ATOMIC_INCREMENT:
{
DataStructuresRequest restReq0 = new DataStructuresRequest();
restReq0.key(params.get("key"));
restReq0.initial(longValue("init", params, null));
restReq0.delta(longValue("delta", params, null));
restReq = restReq0;
break;
}
case CACHE_CONTAINS_KEY:
case CACHE_CONTAINS_KEYS:
case CACHE_GET:
case CACHE_GET_ALL:
case CACHE_GET_AND_PUT:
case CACHE_GET_AND_REPLACE:
case CACHE_PUT_IF_ABSENT:
case CACHE_GET_AND_PUT_IF_ABSENT:
case CACHE_PUT:
case CACHE_PUT_ALL:
case CACHE_REMOVE:
case CACHE_REMOVE_VALUE:
case CACHE_REPLACE_VALUE:
case CACHE_GET_AND_REMOVE:
case CACHE_REMOVE_ALL:
case CACHE_CLEAR:
case CACHE_ADD:
case CACHE_CAS:
case CACHE_METRICS:
case CACHE_SIZE:
case CACHE_METADATA:
case CACHE_REPLACE:
case CACHE_APPEND:
case CACHE_PREPEND:
{
GridRestCacheRequest restReq0 = new GridRestCacheRequest();
String cacheName = (String) params.get(CACHE_NAME_PARAM);
restReq0.cacheName(F.isEmpty(cacheName) ? null : cacheName);
String keyType = (String) params.get("keyType");
String valType = (String) params.get("valueType");
restReq0.key(convert(keyType, params.get("key")));
restReq0.value(convert(valType, params.get("val")));
restReq0.value2(convert(valType, params.get("val2")));
Object val1 = convert(valType, params.get("val1"));
if (val1 != null)
restReq0.value(val1);
// Cache operations via REST will use binary objects.
restReq0.cacheFlags(intValue("cacheFlags", params, KEEP_BINARIES_MASK));
restReq0.ttl(longValue("exp", params, null));
if (cmd == CACHE_GET_ALL || cmd == CACHE_PUT_ALL || cmd == CACHE_REMOVE_ALL || cmd == CACHE_CONTAINS_KEYS) {
List<Object> keys = values(keyType, "k", params);
List<Object> vals = values(valType, "v", params);
if (keys.size() < vals.size())
throw new IgniteCheckedException("Number of keys must be greater or equals to number of values.");
Map<Object, Object> map = U.newHashMap(keys.size());
Iterator<Object> keyIt = keys.iterator();
Iterator<Object> valIt = vals.iterator();
while (keyIt.hasNext()) map.put(keyIt.next(), valIt.hasNext() ? valIt.next() : null);
restReq0.values(map);
}
restReq = restReq0;
break;
}
case TOPOLOGY:
case NODE:
{
GridRestTopologyRequest restReq0 = new GridRestTopologyRequest();
restReq0.includeMetrics(Boolean.parseBoolean((String) params.get("mtr")));
restReq0.includeAttributes(Boolean.parseBoolean((String) params.get("attr")));
restReq0.nodeIp((String) params.get("ip"));
restReq0.nodeId(uuidValue("id", params));
restReq = restReq0;
break;
}
case EXE:
case RESULT:
case NOOP:
{
GridRestTaskRequest restReq0 = new GridRestTaskRequest();
restReq0.taskId((String) params.get("id"));
restReq0.taskName((String) params.get("name"));
restReq0.params(values(null, "p", params));
restReq0.async(Boolean.parseBoolean((String) params.get("async")));
restReq0.timeout(longValue("timeout", params, 0L));
restReq = restReq0;
break;
}
case LOG:
{
GridRestLogRequest restReq0 = new GridRestLogRequest();
restReq0.path((String) params.get("path"));
restReq0.from(intValue("from", params, -1));
restReq0.to(intValue("to", params, -1));
restReq = restReq0;
break;
}
case NAME:
case VERSION:
{
restReq = new GridRestRequest();
break;
}
case CLUSTER_ACTIVE:
case CLUSTER_INACTIVE:
case CLUSTER_CURRENT_STATE:
{
GridRestChangeStateRequest restReq0 = new GridRestChangeStateRequest();
if (cmd == CLUSTER_CURRENT_STATE)
restReq0.reqCurrentState();
else
restReq0.active(cmd == CLUSTER_ACTIVE);
restReq = restReq0;
break;
}
case ADD_USER:
case REMOVE_USER:
case UPDATE_USER:
{
RestUserActionRequest restReq0 = new RestUserActionRequest();
restReq0.user((String) params.get("user"));
restReq0.password((String) params.get("password"));
restReq = restReq0;
break;
}
case EXECUTE_SQL_QUERY:
case EXECUTE_SQL_FIELDS_QUERY:
{
RestQueryRequest restReq0 = new RestQueryRequest();
restReq0.sqlQuery((String) params.get("qry"));
restReq0.arguments(values(null, "arg", params).toArray());
restReq0.typeName((String) params.get("type"));
String pageSize = (String) params.get("pageSize");
if (pageSize != null)
restReq0.pageSize(Integer.parseInt(pageSize));
String distributedJoins = (String) params.get("distributedJoins");
if (distributedJoins != null)
restReq0.distributedJoins(Boolean.parseBoolean(distributedJoins));
restReq0.cacheName((String) params.get(CACHE_NAME_PARAM));
if (cmd == EXECUTE_SQL_QUERY)
restReq0.queryType(RestQueryRequest.QueryType.SQL);
else
restReq0.queryType(RestQueryRequest.QueryType.SQL_FIELDS);
restReq = restReq0;
break;
}
case EXECUTE_SCAN_QUERY:
{
RestQueryRequest restReq0 = new RestQueryRequest();
restReq0.sqlQuery((String) params.get("qry"));
String pageSize = (String) params.get("pageSize");
if (pageSize != null)
restReq0.pageSize(Integer.parseInt(pageSize));
restReq0.cacheName((String) params.get(CACHE_NAME_PARAM));
restReq0.className((String) params.get("className"));
restReq0.queryType(RestQueryRequest.QueryType.SCAN);
restReq = restReq0;
break;
}
case FETCH_SQL_QUERY:
{
RestQueryRequest restReq0 = new RestQueryRequest();
String qryId = (String) params.get("qryId");
if (qryId != null)
restReq0.queryId(Long.parseLong(qryId));
String pageSize = (String) params.get("pageSize");
if (pageSize != null)
restReq0.pageSize(Integer.parseInt(pageSize));
restReq0.cacheName((String) params.get(CACHE_NAME_PARAM));
restReq = restReq0;
break;
}
case CLOSE_SQL_QUERY:
{
RestQueryRequest restReq0 = new RestQueryRequest();
String qryId = (String) params.get("qryId");
if (qryId != null)
restReq0.queryId(Long.parseLong(qryId));
restReq0.cacheName((String) params.get(CACHE_NAME_PARAM));
restReq = restReq0;
break;
}
default:
throw new IgniteCheckedException("Invalid command: " + cmd);
}
restReq.address(new InetSocketAddress(req.getRemoteAddr(), req.getRemotePort()));
restReq.command(cmd);
if (params.containsKey(IGNITE_LOGIN) || params.containsKey(IGNITE_PASSWORD)) {
SecurityCredentials cred = new SecurityCredentials((String) params.get(IGNITE_LOGIN), (String) params.get(IGNITE_PASSWORD));
restReq.credentials(cred);
}
String clientId = (String) params.get("clientId");
try {
if (clientId != null)
restReq.clientId(UUID.fromString(clientId));
} catch (Exception ignored) {
// Ignore invalid client id. Rest handler will process this logic.
}
String destId = (String) params.get("destId");
try {
if (destId != null)
restReq.destinationId(UUID.fromString(destId));
} catch (IllegalArgumentException ignored) {
// Don't fail - try to execute locally.
}
String sesTokStr = (String) params.get("sessionToken");
try {
if (sesTokStr != null)
restReq.sessionToken(U.hexString2ByteArray(sesTokStr));
} catch (IllegalArgumentException ignored) {
// Ignore invalid session token.
}
return restReq;
}
Aggregations