static void transformValues(::KeyValue keyValue, std::pair<std::string, GridClientVariant>& pair) { assert(keyValue.has_key()); assert(keyValue.has_value()); GridClientVariant key; GridClientObjectWrapperConvertor::unwrapSimpleType(keyValue.key(), key); GridClientObjectWrapperConvertor::unwrapSimpleType(keyValue.value(), pair.second); pair.first = key.toString(); }
static bool getBoolValue(const string& binary, GridClientVariant& var) { assert(binary.size() == 1); var.set(binary[0] == 1 ? true : false); return true; }
bool GridClientObjectWrapperConvertor::wrapSimpleType(const GridClientVariant& var, ObjectWrapper& objWrapper) { objWrapper.set_type(NONE); GridClientVariantVisitorImpl visitor(objWrapper); objWrapper.set_binary((void*) NULL, 0); var.accept(visitor); return objWrapper.type() != NONE; }
static bool getInt64Value(const string& binary, GridClientVariant& var) { int64_t res; GridClientByteUtils::bytesToValue((const int8_t*) binary.data(), binary.size(), res); var.set(res); return true; }
static bool getBytesValue(const string& binary, GridClientVariant& var) { std::vector<int8_t> v(binary.size()); std::copy(binary.begin(), binary.end(), v.begin()); var.set(v); return true; }
void GridClientProtobufMarshaller::unwrap(const ObjectWrapper& objWrapper, GridClientMessageCacheModifyResult& msgRes) { ProtoResponse resp; unwrapResponse(objWrapper, resp); fillResponseHeader(resp, msgRes); if (resp.has_resultbean()) { const ObjectWrapper& boolWrapper = resp.resultbean(); GridClientVariant var; GridClientObjectWrapperConvertor::unwrapSimpleType(boolWrapper, var); assert(var.hasBool()); msgRes.setOperationResult(var.getBool()); } else msgRes.setOperationResult(false); }
static bool getFloatValue(const string& binary, GridClientVariant& var) { int32_t res; GridClientByteUtils::bytesToValue((const int8_t*) binary.data(), binary.size(), res); float floatVal = GridFloatHasheableObject::intBitsToFloat(res); var.set(floatVal); return true; }
static bool getDoubleValue(const string& binary, GridClientVariant& var) { int64_t res; GridClientByteUtils::bytesToValue((const int8_t*) binary.data(), binary.size(), res); double doubleVal = GridDoubleHasheableObject::longBitsToDouble(res); var.set(doubleVal); return true; }
static bool doUnwrapSimpleType(const ObjectWrapper& objWrapper, GridClientVariant& var) { assert(objWrapper.has_binary()); GG_LOG_DEBUG("Unwrap simple type: %s", objWrapper.DebugString().c_str()); string binary = objWrapper.binary(); bool unwrapRes = false; switch (objWrapper.type()) { case NONE: return true; case BOOL: return getBoolValue(binary, var); case BYTE: return getByteValue(binary, var); case BYTES: return getBytesValue(binary, var); case INT32: return getInt32Value(binary, var); case INT64: return getInt64Value(binary, var); case SHORT: return getInt16Value(binary, var); case STRING: var.set(binary); return true; case DOUBLE: return getDoubleValue(binary, var); case FLOAT: return getFloatValue(binary, var); case TASK_BEAN: return getTaskTesult(binary, var); default: // Non-simple type break; } return unwrapRes; }
static long doGetValue(const TCacheMetrics& metricsMap, std::string name) { GridClientVariant v(name); TCacheMetrics::const_iterator it = metricsMap.find(name); assert(it != metricsMap.end()); long res = 0; if (it != metricsMap.end()) { GridClientVariant var = it->second; assert(var.hasInt() || var.hasLong()); if (var.hasInt()) res = var.getInt(); else res = (long) var.getLong(); } return res; }
/** * Runs an In-Memory Data Grid client example. * * @param client A client reference. */ void clientDataExample(TGridClientPtr& client) { TGridClientComputePtr cc = client->compute(); TGridClientNodeList nodes = cc->nodes(); if (nodes.empty()) { cerr << "Failed to connect to grid in cache example, make sure that it is started and connection " "properties are correct." << endl; GridClientFactory::stopAll(); return; } cout << "Current grid topology: " << nodes.size() << endl; // Random node ID. GridUuid randNodeId = nodes[0]->getNodeId(); // Get client projection of grid partitioned cache. TGridClientDataPtr rmtCache = client->data(CACHE_NAME); TGridClientVariantSet keys; // Put some values to the cache. for (int32_t i = 0; i < KEYS_CNT; i++) { ostringstream oss; oss << "val-" << i; string v = oss.str(); string key = boost::lexical_cast<string>(i); rmtCache->put(key, v); GridUuid nodeId = rmtCache->affinity(key); cout << ">>> Storing key " << key << " on node " << nodeId << endl; keys.push_back(key); } TGridClientNodeList nodelst; TGridClientNodePtr p = client->compute()->node(randNodeId); nodelst.push_back(p); // Pin a remote node for communication. All further communication // on returned projection will happen through this pinned node. TGridClientDataPtr prj = rmtCache->pinNodes(nodelst); GridClientVariant key0 = GridClientVariant(boost::lexical_cast<string>(0)); GridClientVariant key6 = GridClientVariant(boost::lexical_cast<string>(6)); GridClientVariant val = prj->get(key0); cout << ">>> Loaded single value: " << val.debugString() << endl; TGridClientVariantMap vals = prj->getAll(keys); cout << ">>> Loaded multiple values, size: " << vals.size() << endl; for (TGridClientVariantMap::const_iterator iter = vals.begin(); iter != vals.end(); ++iter) cout << ">>> Loaded cache entry [key=" << iter->first << ", val=" << iter->second << ']' << endl; TGridBoolFuturePtr futPut = prj->putAsync(key0, "new value for 0"); cout << ">>> Result of asynchronous put: " << (futPut->get() ? "success" : "failure") << endl; unordered_map<GridUuid, TGridClientVariantMap> keyVals; GridUuid nodeId = rmtCache->affinity(key0); for (int32_t i = 0; i < KEYS_CNT; i++) { ostringstream oss; oss << "updated-val-" << i; string v = oss.str(); string key = boost::lexical_cast<string>(i); keyVals[nodeId][key] = v; } for (unordered_map<GridUuid, TGridClientVariantMap>::iterator iter = keyVals.begin(); iter != keyVals.end(); ++iter) rmtCache->putAll(iter->second); vector<TGridBoolFuturePtr> futs; for (unordered_map<GridUuid, TGridClientVariantMap>::iterator iter = keyVals.begin(); iter != keyVals.end(); ++iter) { TGridBoolFuturePtr fut = rmtCache->putAllAsync(iter->second); futs.push_back(fut); } for (vector<TGridBoolFuturePtr>::iterator iter = futs.begin(); iter != futs.end(); ++iter) cout << ">>> Result of async putAll: " << (iter->get()->get() ? "success" : "failure") << endl; cout << ">>> Value for key " << key0.debugString() << " is " << rmtCache->get(key0).debugString() << endl; // Asynchronous gets, too. TGridClientFutureVariant futVal = rmtCache->getAsync(key0); cout << ">>> Asynchronous value for key " << key0.debugString() << " is " << futVal->get().debugString() << endl; // Multiple values can be fetched at once. Here we batch our get // requests by affinity nodes to ensure least amount of network trips. for (unordered_map<GridUuid, TGridClientVariantMap>::iterator iter = keyVals.begin(); iter != keyVals.end(); ++iter) { GridUuid uuid = iter->first; TGridClientVariantMap m = iter->second; TGridClientVariantSet keys; for (TGridClientVariantMap::const_iterator miter = m.begin(); miter != m.end(); ++miter ) keys.push_back(miter->first); // Since all keys in our getAll(...) call are mapped to the same primary node, // grid cache client will pick this node for the request, so we only have one // network trip here. TGridClientVariantMap map = rmtCache->getAll(keys); cout << ">>> Values from node [nodeId=" + uuid.uuid() + ", values=[" << endl; for (TGridClientVariantMap::const_iterator miter = map.begin(); miter != map.end(); ++miter) { if (miter != map.begin()) cout << ", " << endl; cout << "[key=" << miter->first << ", value=" << miter->second << ']'; } cout << ']' << endl; } // Multiple values may be retrieved asynchronously, too. // Here we retrieve all keys at once. Since this request // will be sent to some grid node, this node may not be // the primary node for all keys and additional network // trips will have to be made within grid. TGridClientVariantMap map = rmtCache->getAllAsync(keys)->get(); cout << ">>> Values retrieved asynchronously: " << endl; for (TGridClientVariantMap::const_iterator miter = map.begin(); miter != map.end(); ++miter) { if (miter != map.begin()) cout << ", " << endl; cout << "[key=" << miter->first << ", value=" << miter->second << ']'; } cout << endl; // Contents of cache may be removed one by one synchronously. // Again, this operation is affinity aware and only the primary // node for the key is contacted. bool res = rmtCache->remove(key0); cout << ">>> Result of removal: " << (res ? "success" : "failure") << endl; // ... and asynchronously. TGridBoolFuturePtr futRes = rmtCache->removeAsync(boost::lexical_cast<string>(1)); cout << ">>> Result of asynchronous removal: " << (futRes->get() ? "success" : "failure") << endl; // Multiple entries may be removed at once synchronously... TGridClientVariantSet keysRemove; keysRemove.push_back(boost::lexical_cast<string>(2)); keysRemove.push_back(boost::lexical_cast<string>(3)); bool rmvRslt = rmtCache->removeAll(keysRemove); cout << ">>> Result of removeAll: " << (rmvRslt ? "success" : "failure") << endl; // ... and asynchronously. keysRemove.clear(); keysRemove.push_back(boost::lexical_cast<string>(4)); keysRemove.push_back(boost::lexical_cast<string>(5)); TGridBoolFuturePtr rmvFut = rmtCache->removeAllAsync(keysRemove); cout << ">>> Result of asynchronous removeAll: " << (rmvFut->get() ? "success" : "failure") << endl; // Values may also be replaced. res = rmtCache->replace(key6, "newer value for 6"); cout << ">>> Result for replace for existent key6 is " << (res ? "success" : "failure") << endl; // Asynchronous replace is supported, too. This one is expected to fail, since key0 doesn't exist. futRes = rmtCache->replaceAsync(key0, "newest value for 0"); res = futRes->get(); cout << ">>> Result for asynchronous replace for nonexistent key0 is " << (res ? "success" : "failure") << endl; rmtCache->put(key0, boost::lexical_cast<string>(0)); // Compare and set are implemented, too. res = rmtCache->cas(key0, "newest cas value for 0", boost::lexical_cast<string>(0)); cout << ">>> Result for put using cas is " << (res ? "success" : "failure") << endl; // CAS can be asynchronous. futRes = rmtCache->casAsync(key0, boost::lexical_cast<string>(0), "newest cas value for 0"); res = futRes->get(); // Expected to fail. cout << ">>> Result for put using asynchronous cas is " << (res? "success" : "failure") << endl; // It's possible to obtain cache metrics using data client API. GridClientDataMetrics metrics = rmtCache->metrics(); cout << ">>> Cache metrics : " << metrics << endl; TGridClientFutureDataMetrics futMetrics = rmtCache->metricsAsync(); cout << ">>> Cache asynchronous metrics: " << futMetrics->get() << endl; key0 = GridClientVariant("0"); GridClientVariant val0("new value for 0"); rmtCache->put(key0, val0); randNodeId = nodes[0]->getNodeId(); TGridClientComputePtr clientCompute = client->compute(); cout << "RandNodeId is " << randNodeId.uuid() << endl; p = clientCompute->node(randNodeId); TGridClientComputePtr compPrj = clientCompute->projection(*p); GridClientVariant rslt = compPrj->affinityExecute("org.gridgain.examples.client.api.GridClientExampleTask", CACHE_NAME, key0); cout << ">>> Affinity execute : there are totally " << rslt.toString() << " test entries on the grid" << endl; futVal = compPrj->affinityExecuteAsync("org.gridgain.examples.client.api.GridClientExampleTask", CACHE_NAME, key0); cout << ">>> Affinity execute async : there are totally " << futVal->get().toString() << " test entries on the grid" << endl; }
void clientComputeExample(TGridClientPtr client) { TGridClientComputePtr clientCompute = client->compute(); TGridClientNodeList nodes = clientCompute->nodes(); if (nodes.empty()) { cerr << "Failed to connect to grid in compute example, make sure that it is started and connection " "properties are correct." << endl; GridClientFactory::stopAll(); return; } cout << "Current grid topology: " << nodes.size() << endl; GridUuid randNodeId = nodes[0]->getNodeId(); cout << "RandNodeId is " << randNodeId.uuid() << endl; TGridClientNodePtr p = clientCompute->node(randNodeId); TGridClientComputePtr prj = clientCompute->projection(*p); GridClientVariant rslt = prj->execute("org.gridgain.examples.client.GridClientExampleTask"); cout << ">>> GridClientNode projection : there are totally " << rslt.toString() << " test entries on the grid" << endl; TGridClientNodeList prjNodes; prjNodes.push_back(p); prj = clientCompute->projection(prjNodes); rslt = prj->execute("org.gridgain.examples.client.GridClientExampleTask"); cout << ">>> Collection execution : there are totally " << rslt.toString() << " test entries on the grid" << endl; GridUuidNodePredicate* uuidPredicate = new GridUuidNodePredicate(randNodeId); TGridClientNodePredicatePtr predPtr(uuidPredicate); prj = clientCompute->projection(predPtr); rslt = prj->execute("org.gridgain.examples.client.GridClientExampleTask"); cout << ">>> Predicate execution : there are totally " << rslt.toString() << " test entries on the grid" << endl; // Balancing - may be random or round-robin. Users can create // custom load balancers as well. TGridClientLoadBalancerPtr balancer(new GridClientRandomBalancer()); prj = clientCompute->projection(predPtr, balancer); rslt = prj->execute("org.gridgain.examples.client.GridClientExampleTask"); cout << ">>> Predicate execution with balancer : there are totally " << rslt.toString() << " test entries on the grid" << endl; // Now let's try round-robin load balancer. balancer = TGridClientLoadBalancerPtr(new GridClientRoundRobinBalancer()); prj = prj->projection(prjNodes, balancer); rslt = prj->execute("org.gridgain.examples.client.GridClientExampleTask"); cout << ">>> GridClientNode projection : there are totally " << rslt.toString() << " test entries on the grid" << endl; TGridClientFutureVariant futVal = prj->executeAsync("org.gridgain.examples.client.GridClientExampleTask"); futVal->get(); if (futVal->success()) cout << ">>> Execute async : there are totally " << futVal->result().toString() << " test entries on the grid" << endl; else cout << ">>> Execute async failed" << endl; TGridClientDataPtr rmtCache = client->data("partitioned"); GridClientVariant key0((int32_t)0); GridClientVariant val0("new value for 0"); rmtCache->put(key0, val0); rslt = prj->affinityExecute("org.gridgain.examples.client.GridClientExampleTask", "partitioned", key0); cout << ">>> Affinity execute : there are totally " << rslt.toString() << " test entries on the grid" << endl; futVal = prj->affinityExecuteAsync("org.gridgain.examples.client.GridClientExampleTask", "partitioned", key0); futVal->get(); if (futVal->success()) cout << ">>> Affinity execute async : there are totally " << futVal->result().toString() << " test entries on the grid" << endl; else cout << ">>> Affinity execute async failed" << endl; vector<GridUuid> uuids; uuids.push_back(randNodeId); nodes = prj->nodes(uuids); cout << ">>> Nodes with UUID " << randNodeId.uuid() << " : "; for (size_t i = 0 ; i < nodes.size(); i++) { if (i != 0) cout << ", "; cout << *(nodes[i]); } cout << endl; // Nodes may also be filtered with predicate. Here // we create projection which only contains local node. GridUuidNodePredicate* uuidNodePredicate = new GridUuidNodePredicate(randNodeId); TGridClientNodePredicatePtr predFullPtr(uuidNodePredicate); nodes = prj->nodes(predFullPtr); cout << ">>> Nodes filtered with predicate : "; for (size_t i = 0 ; i < nodes.size(); i++) { if (i != 0) cout << ", "; cout << *(nodes[i]); } cout << endl; // Information about nodes may be refreshed explicitly. TGridClientNodePtr clntNode = prj->refreshNode(randNodeId, true, true); cout << ">>> Refreshed node : " << *clntNode << endl; TGridClientNodeFuturePtr futClntNode = prj->refreshNodeAsync(randNodeId, false, false); futClntNode->get(); if (futClntNode->success()) cout << ">>> Refreshed node asynchronously : " << *(futClntNode->result()) << endl; else cout << ">>> Refresh node asynchronously failed." << endl; // Nodes may also be refreshed by IP address. string clntAddr = "127.0.0.1"; vector<GridSocketAddress> addrs = clntNode->availableAddresses(TCP); if (addrs.size() > 0) clntAddr = addrs[0].host(); clntNode = prj->refreshNode(clntAddr, true, true); cout << ">>> Refreshed node by IP : " << *clntNode << endl; // Asynchronous version. futClntNode = prj->refreshNodeAsync(clntAddr, false, false); futClntNode->get(); if (futClntNode->success()) cout << ">>> Refreshed node by IP asynchronously : " << *futClntNode->result() << endl; else cout << ">>> Refreshed node by IP asynchronously failed." << endl; // Topology as a whole may be refreshed, too. TGridClientNodeList top = prj->refreshTopology(true, true); cout << ">>> Refreshed topology : "; for (size_t i = 0 ; i < top.size(); i++) { if (i != 0) cout << ", "; cout << *top[i]; } cout << endl; // Asynchronous version. TGridClientNodeFutureList topFut = prj->refreshTopologyAsync(false, false); topFut->get(); if (topFut->success()) { cout << ">>> Refreshed topology asynchronously : "; top = topFut->result(); for (size_t i = 0 ; i < top.size(); i++) { if (i != 0) cout << ", "; cout << *top[i]; } cout << endl; } else cout << ">>> Refresh topology asynchronously failed." << endl; try { vector<string> log = prj->log(0, 1); cout << ">>> First log lines : " << endl; cout << log[0] << endl; cout << log[1] << endl; // Log entries may be fetched asynchronously. TGridFutureStringList futLog = prj->logAsync(1, 2); futLog->get(); if (futLog->success()) { log = futLog->result(); cout << ">>> First log lines fetched asynchronously : " << endl; cout << log[0] << endl; cout << log[1] << endl; } else cout << ">>> First log lines fetching asynchronously failed " << endl; // Log file name can also be specified explicitly. log = prj->log("work/log/gridgain.log", 0, 1); cout << ">>> First log lines from log file work/log/gridgain.log : " << endl; cout << log[0] << endl; cout << log[1] << endl; // Asynchronous version supported as well. futLog = prj->logAsync("work/log/gridgain.log", 1, 2); futLog->get(); if (futLog->success()) { log = futLog->result(); cout << ">>> First log lines from log file work/log/gridgain.log fetched asynchronously : " << endl; cout << log[0] << endl; cout << log[1] << endl; } else cout << ">>> First log lines from log file work/log/gridgain.log fetching asynchronously failed " << endl; } catch (GridClientException&) { cout << "Log file was not found " << endl; } cout << "In the end of example" << endl; }
/** * Thread proc for marshalling specific types of messaging. * * @param messageType Type of messages to marshal. */ void run(ObjectWrapperType messageType) { using namespace org::gridgain::grid::kernal::processors::rest::client::message; iters = 0; try { bool unmarshal = vm["unmarshal"].as<bool>(); ObjectWrapper marshalledObject; int maxiterations = vm["nummessages"].as<int>(); if ((messageType > NONE && messageType <= STRING) || messageType == UUID) { GridClientVariant value; switch (messageType) { case BOOL: value = true; break; case BYTE: value = (int8_t) 42; break; case SHORT: value = (int16_t) 42; break; case INT32: value = (int32_t) 42; break; case INT64: value = (int64_t) 42; break; case FLOAT: value = (float) 0.42f; break; case DOUBLE: value = (double) 0.42f; break; case STRING: value = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; break; case UUID: { // block of code to avoid compilation error in gcc value.set(GridClientUuid("550e8400-e29b-41d4-a716-446655440000")); if (unmarshal) std::cerr << "Unmarshalling UUID is not supported yet"; } break; default: std::cerr << "Unsupported message type.\n"; return; } while (++iters != maxiterations) { GridClientObjectWrapperConvertor::wrapSimpleType(value, marshalledObject); if (unmarshal && messageType != UUID) { value.clear(); GridClientObjectWrapperConvertor::unwrapSimpleType(marshalledObject, value); } } } else { switch (messageType) { case CACHE_REQUEST: { // block of code to avoid compilation error in gcc GridCacheRequestCommand cmd = GridCacheRequestCommand( GridCacheRequestCommand::GridCacheOperation::PUT); cmd.sessionToken("Something"); cmd.setCacheName("partitioned"); cmd.setClientId(GridClientUuid("550e8400-e29b-41d4-a716-446655440000")); cmd.setDestinationId(GridClientUuid("550e8400-e29b-41d4-a716-446655440000")); cmd.setKey(42.0f); cmd.setRequestId(42); cmd.setValue(42.0f); // typical reply for cache PUT. 15 bytes length; unsigned char daReply[] = { 8, 0, 26, 5, 8, 1, 18, 1, 1, 34, 0 }; GridClientMessageCacheModifyResult resp; while (++iters != maxiterations) { GridClientProtobufMarshaller::wrap(cmd, marshalledObject); if (unmarshal) { marshalledObject.set_binary(daReply, sizeof(daReply)); marshalledObject.set_type(RESPONSE); GridClientProtobufMarshaller::unwrap(marshalledObject, resp); } } } break; case TOPOLOGY_REQUEST: { // block of code to avoid compilation error in gcc GridTopologyRequestCommand cmd; // typical reply for topology. Contains info about 2 local nodes. 185 bytes length; unsigned char daReply[] = { 8, 0, 26, 173, 1, 8, 10, 18, 168, 1, 10, 165, 1, 8, 60, 18, 160, 1, 10, 16, 6, 225, 6, 102, 61, 76, 77, 123, 129, 135, 99, 184, 120, 177, 249, 186, 18, 9, 49, 50, 55, 46, 48, 46, 48, 46, 49, 26, 9, 49, 50, 55, 46, 48, 46, 48, 46, 49, 32, 224, 78, 40, 144, 63, 74, 88, 10, 25, 10, 6, 8, 9, 18, 2, 116, 120, 18, 15, 8, 9, 18, 11, 80, 65, 82, 84, 73, 84, 73, 79, 78, 69, 68, 10, 28, 10, 9, 8, 9, 18, 5, 113, 117, 101, 114, 121, 18, 15, 8, 9, 18, 11, 80, 65, 82, 84, 73, 84, 73, 79, 78, 69, 68, 10, 29, 10, 10, 8, 9, 18, 6, 97, 116, 111, 109, 105, 99, 18, 15, 8, 9, 18, 11, 80, 65, 82, 84, 73, 84, 73, 79, 78, 69, 68, 104, 128, 1, 114, 19, 8, 9, 18, 15, 49, 50, 55, 46, 48, 46, 48, 46, 49, 58, 52, 55, 53, 48, 48, 34, 0 }; GridClientMessageTopologyResult resp; while (++iters != maxiterations) { GridClientProtobufMarshaller::wrap(cmd, marshalledObject); if (unmarshal) { marshalledObject.set_binary(daReply, sizeof(daReply)); marshalledObject.set_type(RESPONSE); GridClientProtobufMarshaller::unwrap(marshalledObject, resp); } } } break; default: std::cerr << "Unsupported message type.\n"; return; } } } catch (GridClientException& e) { std::cerr << "GridClientException: " << e.what() << "\n"; exit(1); } catch (...) { std::cerr << "Unknown exception.\n"; exit(1); } }