use of org.apache.ignite.IgniteCache in project ignite by apache.
the class IgniteDynamicCacheConfigTest method testGetOrCreateNearCache.
/**
* @throws Exception If failed.
*/
public void testGetOrCreateNearCache() throws Exception {
testAttribute = false;
IgniteCache cache = ignite(0).createCache(load("modules/spring/src/test/java/org/apache/ignite/internal/filtered-cache.xml"));
try {
int clientNode = nodeCount() + 1;
startGrid(clientNode);
IgniteCache cache1 = ignite(clientNode).getOrCreateCache(load("modules/spring/src/test/java/org/apache/ignite/internal/filtered-cache.xml"), loadNear("modules/spring/src/test/java/org/apache/ignite/internal/cache.xml"));
assertEquals(cache.getName(), cache1.getName());
IgniteCache clientCache1 = ignite(clientNode).createNearCache(CACHE_NAME, loadNear("modules/spring/src/test/java/org/apache/ignite/internal/cache.xml"));
IgniteCache clientCache2 = ignite(clientNode).getOrCreateNearCache(CACHE_NAME, loadNear("modules/spring/src/test/java/org/apache/ignite/internal/cache.xml"));
assertEquals(clientCache1, clientCache2);
clientCache1.put(1, 1);
} finally {
stopGrid(nodeCount() + 1);
}
}
use of org.apache.ignite.IgniteCache in project ignite by apache.
the class AlgorithmSpecificDatasetExample method main.
/**
* Run example.
*/
public static void main(String[] args) throws Exception {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Algorithm Specific Dataset example started.");
IgniteCache<Integer, Person> persons = createCache(ignite);
// labels are extracted, and partition data and context are created.
try (AlgorithmSpecificDataset dataset = DatasetFactory.create(ignite, persons, (upstream, upstreamSize) -> new AlgorithmSpecificPartitionContext(), new SimpleLabeledDatasetDataBuilder<Integer, Person, AlgorithmSpecificPartitionContext>((k, v) -> new double[] { v.getAge() }, (k, v) -> v.getSalary(), 1).andThen((data, ctx) -> {
double[] features = data.getFeatures();
int rows = data.getRows();
// Makes a copy of features to supplement it by columns with values equal to 1.0.
double[] a = new double[features.length + rows];
for (int i = 0; i < rows; i++) a[i] = 1.0;
System.arraycopy(features, 0, a, rows, features.length);
return new SimpleLabeledDatasetData(a, rows, data.getCols() + 1, data.getLabels());
})).wrap(AlgorithmSpecificDataset::new)) {
// Trains linear regression model using gradient descent.
double[] linearRegressionMdl = new double[2];
for (int i = 0; i < 1000; i++) {
double[] gradient = dataset.gradient(linearRegressionMdl);
if (BLAS.getInstance().dnrm2(gradient.length, gradient, 1) < 1e-4)
break;
for (int j = 0; j < gradient.length; j++) linearRegressionMdl[j] -= 0.1 / persons.size() * gradient[j];
}
System.out.println("Linear Regression Model: " + Arrays.toString(linearRegressionMdl));
}
System.out.println(">>> Algorithm Specific Dataset example completed.");
}
}
use of org.apache.ignite.IgniteCache in project ignite by apache.
the class CacheContinuousQueryExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
* @throws Exception If example execution failed.
*/
public static void main(String[] args) throws Exception {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println();
System.out.println(">>> Cache continuous query example started.");
// Auto-close cache at the end of the example.
try (IgniteCache<Integer, String> cache = ignite.getOrCreateCache(CACHE_NAME)) {
int keyCnt = 20;
// These entries will be queried by initial predicate.
for (int i = 0; i < keyCnt; i++) cache.put(i, Integer.toString(i));
// Create new continuous query.
ContinuousQuery<Integer, String> qry = new ContinuousQuery<>();
qry.setInitialQuery(new ScanQuery<>(new IgniteBiPredicate<Integer, String>() {
@Override
public boolean apply(Integer key, String val) {
return key > 10;
}
}));
// Callback that is called locally when update notifications are received.
qry.setLocalListener(new CacheEntryUpdatedListener<Integer, String>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends String>> evts) {
for (CacheEntryEvent<? extends Integer, ? extends String> e : evts) System.out.println("Updated entry [key=" + e.getKey() + ", val=" + e.getValue() + ']');
}
});
// This filter will be evaluated remotely on all nodes.
// Entry that pass this filter will be sent to the caller.
qry.setRemoteFilterFactory(new Factory<CacheEntryEventFilter<Integer, String>>() {
@Override
public CacheEntryEventFilter<Integer, String> create() {
return new CacheEntryEventFilter<Integer, String>() {
@Override
public boolean evaluate(CacheEntryEvent<? extends Integer, ? extends String> e) {
return e.getKey() > 10;
}
};
}
});
// Execute query.
try (QueryCursor<Cache.Entry<Integer, String>> cur = cache.query(qry)) {
// Iterate through existing data.
for (Cache.Entry<Integer, String> e : cur) System.out.println("Queried existing entry [key=" + e.getKey() + ", val=" + e.getValue() + ']');
// Add a few more keys and watch more query notifications.
for (int i = keyCnt; i < keyCnt + 10; i++) cache.put(i, Integer.toString(i));
// Wait for a while while callback is notified about remaining puts.
Thread.sleep(2000);
}
} finally {
// Distributed cache could be removed from cluster only by #destroyCache() call.
ignite.destroyCache(CACHE_NAME);
}
}
}
use of org.apache.ignite.IgniteCache in project ignite by apache.
the class GridManagerAdapter method onKernalStart.
/**
* {@inheritDoc}
*/
@Override
public final void onKernalStart(boolean active) throws IgniteCheckedException {
for (final IgniteSpi spi : spis) {
try {
spi.onContextInitialized(new IgniteSpiContext() {
@Override
public boolean isStopping() {
return ctx.isStopping();
}
@Override
public Collection<ClusterNode> remoteNodes() {
return ctx.discovery().remoteNodes();
}
@Override
public Collection<ClusterNode> nodes() {
return ctx.discovery().allNodes();
}
@Override
public ClusterNode localNode() {
return ctx.discovery().localNode();
}
@Override
public Collection<ClusterNode> remoteDaemonNodes() {
final Collection<ClusterNode> all = ctx.discovery().daemonNodes();
return !localNode().isDaemon() ? all : F.view(all, new IgnitePredicate<ClusterNode>() {
@Override
public boolean apply(ClusterNode n) {
return n.isDaemon();
}
});
}
@Nullable
@Override
public ClusterNode node(UUID nodeId) {
A.notNull(nodeId, "nodeId");
return ctx.discovery().node(nodeId);
}
@Override
public boolean pingNode(UUID nodeId) {
A.notNull(nodeId, "nodeId");
try {
return ctx.discovery().pingNode(nodeId);
} catch (IgniteCheckedException e) {
throw U.convertException(e);
}
}
@Override
public void send(ClusterNode node, Serializable msg, String topic) throws IgniteSpiException {
A.notNull(node, "node");
A.notNull(msg, "msg");
A.notNull(topic, "topic");
try {
if (msg instanceof Message)
ctx.io().sendToCustomTopic(node, topic, (Message) msg, SYSTEM_POOL);
else
ctx.io().sendUserMessage(Collections.singletonList(node), msg, topic, false, 0, false);
} catch (IgniteCheckedException e) {
throw unwrapException(e);
}
}
@Override
public void addLocalMessageListener(Object topic, IgniteBiPredicate<UUID, ?> p) {
A.notNull(topic, "topic");
A.notNull(p, "p");
ctx.io().addUserMessageListener(topic, p);
}
@Override
public void removeLocalMessageListener(Object topic, IgniteBiPredicate<UUID, ?> p) {
A.notNull(topic, "topic");
A.notNull(topic, "p");
ctx.io().removeUserMessageListener(topic, p);
}
@SuppressWarnings("deprecation")
@Override
public void addMessageListener(GridMessageListener lsnr, String topic) {
A.notNull(lsnr, "lsnr");
A.notNull(topic, "topic");
ctx.io().addMessageListener(topic, lsnr);
}
@SuppressWarnings("deprecation")
@Override
public boolean removeMessageListener(GridMessageListener lsnr, String topic) {
A.notNull(lsnr, "lsnr");
A.notNull(topic, "topic");
return ctx.io().removeMessageListener(topic, lsnr);
}
@Override
public void addLocalEventListener(GridLocalEventListener lsnr, int... types) {
A.notNull(lsnr, "lsnr");
ctx.event().addLocalEventListener(lsnr, types);
}
@Override
public boolean removeLocalEventListener(GridLocalEventListener lsnr) {
A.notNull(lsnr, "lsnr");
return ctx.event().removeLocalEventListener(lsnr);
}
@Override
public boolean isEventRecordable(int... types) {
for (int t : types) if (!ctx.event().isRecordable(t))
return false;
return true;
}
@Override
public void recordEvent(Event evt) {
A.notNull(evt, "evt");
if (ctx.event().isRecordable(evt.type()))
ctx.event().record(evt);
}
@Override
public void registerPort(int port, IgnitePortProtocol proto) {
ctx.ports().registerPort(port, proto, spi.getClass());
}
@Override
public void deregisterPort(int port, IgnitePortProtocol proto) {
ctx.ports().deregisterPort(port, proto, spi.getClass());
}
@Override
public void deregisterPorts() {
ctx.ports().deregisterPorts(spi.getClass());
}
@Nullable
@Override
public <K, V> V get(String cacheName, K key) {
return ctx.cache().<K, V>jcache(cacheName).get(key);
}
@Nullable
@Override
public <K, V> V put(String cacheName, K key, V val, long ttl) {
try {
if (ttl > 0) {
ExpiryPolicy plc = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
IgniteCache<K, V> cache = ctx.cache().<K, V>publicJCache(cacheName).withExpiryPolicy(plc);
return cache.getAndPut(key, val);
} else
return ctx.cache().<K, V>jcache(cacheName).getAndPut(key, val);
} catch (IgniteCheckedException e) {
throw CU.convertToCacheException(e);
}
}
@Nullable
@Override
public <K, V> V putIfAbsent(String cacheName, K key, V val, long ttl) {
try {
if (ttl > 0) {
ExpiryPolicy plc = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
IgniteCache<K, V> cache = ctx.cache().<K, V>publicJCache(cacheName).withExpiryPolicy(plc);
return cache.getAndPutIfAbsent(key, val);
} else
return ctx.cache().<K, V>jcache(cacheName).getAndPutIfAbsent(key, val);
} catch (IgniteCheckedException e) {
throw CU.convertToCacheException(e);
}
}
@Nullable
@Override
public <K, V> V remove(String cacheName, K key) {
return ctx.cache().<K, V>jcache(cacheName).getAndRemove(key);
}
@Override
public <K> boolean containsKey(String cacheName, K key) {
return ctx.cache().cache(cacheName).containsKey(key);
}
@Override
public int partition(String cacheName, Object key) {
return ctx.cache().cache(cacheName).affinity().partition(key);
}
@Override
public IgniteNodeValidationResult validateNode(ClusterNode node) {
for (GridComponent comp : ctx) {
IgniteNodeValidationResult err = comp.validateNode(node);
if (err != null)
return err;
}
return null;
}
@Nullable
@Override
public IgniteNodeValidationResult validateNode(ClusterNode node, DiscoveryDataBag discoData) {
for (GridComponent comp : ctx) {
if (comp.discoveryDataType() == null)
continue;
IgniteNodeValidationResult err = comp.validateNode(node, discoData.newJoinerDiscoveryData(comp.discoveryDataType().ordinal()));
if (err != null)
return err;
}
return null;
}
@Override
public Collection<SecuritySubject> authenticatedSubjects() {
try {
return ctx.security().authenticatedSubjects();
} catch (IgniteCheckedException e) {
throw U.convertException(e);
}
}
@Override
public SecuritySubject authenticatedSubject(UUID subjId) {
try {
return ctx.security().authenticatedSubject(subjId);
} catch (IgniteCheckedException e) {
throw U.convertException(e);
}
}
@Override
public MessageFormatter messageFormatter() {
return ctx.io().formatter();
}
@Override
public MessageFactory messageFactory() {
return ctx.io().messageFactory();
}
@Override
public boolean tryFailNode(UUID nodeId, @Nullable String warning) {
return ctx.discovery().tryFailNode(nodeId, warning);
}
@Override
public void failNode(UUID nodeId, @Nullable String warning) {
ctx.discovery().failNode(nodeId, warning);
}
@Override
public void addTimeoutObject(IgniteSpiTimeoutObject obj) {
ctx.timeout().addTimeoutObject(new GridSpiTimeoutObject(obj));
}
@Override
public void removeTimeoutObject(IgniteSpiTimeoutObject obj) {
ctx.timeout().removeTimeoutObject(new GridSpiTimeoutObject(obj));
}
@Override
public Map<String, Object> nodeAttributes() {
return ctx.nodeAttributes();
}
/**
* @param e Exception to handle.
* @return GridSpiException Converted exception.
*/
private IgniteSpiException unwrapException(IgniteCheckedException e) {
// Avoid double-wrapping.
if (e.getCause() instanceof IgniteSpiException)
return (IgniteSpiException) e.getCause();
return new IgniteSpiException("Failed to execute SPI context method.", e);
}
});
} catch (IgniteSpiException e) {
throw new IgniteCheckedException("Failed to initialize SPI context.", e);
}
}
onKernalStart0();
}
use of org.apache.ignite.IgniteCache in project ignite by apache.
the class IgniteDiagnosticMessagesTest method testSeveralLongRunningTxs.
/**
* @throws Exception If failed.
*/
public void testSeveralLongRunningTxs() throws Exception {
int timeout = 3500;
System.setProperty(IGNITE_LONG_OPERATIONS_DUMP_TIMEOUT, String.valueOf(timeout));
try {
testSpi = true;
startGrid(0);
GridStringLogger strLog = this.strLog = new GridStringLogger();
strLog.logLength(1024 * 100);
startGrid(1);
awaitPartitionMapExchange();
CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
ccfg.setWriteSynchronizationMode(FULL_SYNC);
ccfg.setCacheMode(PARTITIONED);
ccfg.setAtomicityMode(TRANSACTIONAL);
final Ignite node0 = ignite(0);
final Ignite node1 = ignite(1);
node0.createCache(ccfg);
UUID id0 = node0.cluster().localNode().id();
TestRecordingCommunicationSpi.spi(node0).blockMessages(GridNearLockResponse.class, node1.name());
IgniteCache<Object, Object> cache = node0.cache(DEFAULT_CACHE_NAME);
int txCnt = 4;
final List<Integer> keys = primaryKeys(cache, txCnt, 0);
final AtomicInteger idx = new AtomicInteger();
IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
IgniteCache<Object, Object> cache = node1.cache(DEFAULT_CACHE_NAME);
try (Transaction tx = node1.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
Integer key = keys.get(idx.getAndIncrement() % keys.size());
cache.putIfAbsent(key, String.valueOf(key));
tx.commit();
}
return null;
}
}, txCnt * 2, "tx");
U.sleep(timeout * 2);
assertFalse(fut.isDone());
TestRecordingCommunicationSpi.spi(node0).stopBlock();
fut.get();
String log = strLog.toString();
assertTrue(log.contains("Cache entries [cacheId=" + CU.cacheId(DEFAULT_CACHE_NAME) + ", cacheName=" + DEFAULT_CACHE_NAME + "]:"));
assertTrue(countTxKeysInASingleBlock(log) == txCnt);
assertTrue(log.contains("General node info [id=" + id0));
} finally {
System.clearProperty(IGNITE_LONG_OPERATIONS_DUMP_TIMEOUT);
}
}
Aggregations