@Override @SuppressWarnings("unchecked") public IPartitioner getCassandraPartitioner() throws BackendException { try { return StorageService.getPartitioner(); } catch (Exception e) { log.warn("Could not read local token range: {}", e); throw new PermanentBackendException("Could not read partitioner information on cluster", e); } }
public static synchronized void start(String config) { if (started) { if (null != config && !config.equals(activeConfig)) { log.warn("Can't start in-process Cassandra instance " + "with yaml path {} because an instance was " + "previously started with yaml path {}", config, activeConfig); } return; } started = true; log.debug("Current working directory: {}", System.getProperty("user.dir")); System.setProperty("cassandra.config", config); // Prevent Cassandra from closing stdout/stderr streams System.setProperty("cassandra-foreground", "yes"); // Prevent Cassandra from overwriting Log4J configuration System.setProperty("log4j.defaultInitOverride", "false"); log.info("Starting cassandra with {}", config); /* * This main method doesn't block for any substantial length of time. It * creates and starts threads and returns in relatively short order. */ CassandraDaemon.main(new String[0]); activeConfig = config; }
public static void robustInsert(ConsistencyLevel cl, RowMutation... mutations) { int attempts = 0; while (attempts++ < retryAttempts) { try { StorageProxy.mutate(Arrays.asList(mutations), cl); return; } catch (UnavailableException e) { } catch (TimeoutException e) { } try { Thread.sleep(retryAttemptSleep); } catch (InterruptedException e) { } } throw new RuntimeException("insert failed after 10 attempts"); }
System.setProperty("cassandra.native.epoll.enabled", "false"); CassandraDaemon cassandraDaemon = new CassandraDaemon(); cassandraDaemon.activate(); cassandraDaemon.deactivate(); throw e;
private void mutate(List<org.apache.cassandra.db.Mutation> cmds, org.apache.cassandra.db.ConsistencyLevel clvl) throws BackendException { try { schedule(DatabaseDescriptor.getRpcTimeout()); try { if (atomicBatch) { StorageProxy.mutateAtomically(cmds, clvl); } else { StorageProxy.mutate(cmds, clvl); } } catch (RequestExecutionException e) { throw new TemporaryBackendException(e); } finally { release(); } } catch (TimeoutException ex) { log.debug("Cassandra TimeoutException", ex); throw new TemporaryBackendException(ex); } }
rows = StorageProxy.read(Arrays.asList(rc), cl); return rows;
public String getToken() { if (StorageService.instance.isClientMode()) return CassandraUtils.fakeToken; return StorageService.instance.getTokenMetadata().getToken(FBUtilities.getLocalAddress()).toString(); }
int pageSize, long nowMillis) throws BackendException { IPartitioner partitioner = StorageService.getPartitioner(); rows = StorageProxy.getRangeSlice(cmd, ConsistencyLevel.QUORUM); } catch (Exception e) { throw new PermanentBackendException(e);
private void ensureKeyspaceExists(String keyspaceName) throws BackendException { if (null != Schema.instance.getKeyspaceInstance(keyspaceName)) return; // Keyspace not found; create it String strategyName = storageConfig.get(REPLICATION_STRATEGY); KSMetaData ksm; try { ksm = KSMetaData.newKeyspace(keyspaceName, strategyName, strategyOptions, true); } catch (ConfigurationException e) { throw new PermanentBackendException("Failed to instantiate keyspace metadata for " + keyspaceName, e); } try { MigrationManager.announceNewKeyspace(ksm); log.info("Created keyspace {}", keyspaceName); } catch (ConfigurationException e) { throw new PermanentBackendException("Failed to create keyspace " + keyspaceName, e); } }
public List<KeyRange> getLocalKeyPartition() throws BackendException { ensureKeyspaceExists(keySpaceName); @SuppressWarnings("rawtypes") Collection<Range<Token>> ranges = StorageService.instance.getPrimaryRanges(keySpaceName); List<KeyRange> keyRanges = new ArrayList<KeyRange>(ranges.size()); for (@SuppressWarnings("rawtypes") Range<Token> range : ranges) { keyRanges.add(CassandraHelper.transformRange(range)); } return keyRanges; }
private InetAddress getIndexLocation(String subIndex) { ByteBuffer indexName = CassandraUtils.hashBytes(subIndex.getBytes()); List<InetAddress> endpoints = StorageService.instance.getLiveNaturalEndpoints(CassandraUtils.keySpace, indexName); if (endpoints.isEmpty()) throw new RuntimeException("Unable to find a live endpoint for: " + subIndex); DatabaseDescriptor.getEndpointSnitch().sortByProximity(FBUtilities.getLocalAddress(), endpoints); if (endpoints.contains(FBUtilities.getLocalAddress())) return FBUtilities.getLocalAddress(); return endpoints.get(0); }
public static synchronized void startupClient() throws IOException { if (cassandraStarted) return; cassandraStarted = true; try { System.setProperty("cassandra-foreground", "1"); StorageService.instance.initClient(); logger.info("Started Solandra in client mode... waiting for gossip information"); Thread.sleep(10000); // createCassandraSchema(); } catch (IOException e2) { e2.printStackTrace(); System.exit(2); } catch (ConfigurationException e2) { e2.printStackTrace(); System.exit(2); } catch (InterruptedException e) { e.printStackTrace(); System.exit(2); } return; }
@Override public void clearStorage() throws BackendException { openStores.clear(); try { KSMetaData ksMetaData = Schema.instance.getKSMetaData(keySpaceName); // Not a big deal if Keyspace doesn't not exist (dropped manually by user or tests). // This is called on per test setup basis to make sure that previous test cleaned // everything up, so first invocation would always fail as Keyspace doesn't yet exist. if (ksMetaData == null) return; for (String cfName : ksMetaData.cfMetaData().keySet()) StorageService.instance.truncate(keySpaceName, cfName); } catch (Exception e) { throw new PermanentBackendException(e); } }
private static List<Row> read(List<ReadCommand> cmds, org.apache.cassandra.db.ConsistencyLevel clvl) throws BackendException { try { return StorageProxy.read(cmds, clvl); } catch (UnavailableException e) { throw new TemporaryBackendException(e); } catch (RequestTimeoutException e) { throw new PermanentBackendException(e); } catch (IsBootstrappingException e) { throw new TemporaryBackendException(e); } catch (InvalidRequestException e) { throw new PermanentBackendException(e); } }
private final boolean hasNextInternal() throws BackendException { ensureOpen(); if (keys == null) return false; boolean hasNext = keys.hasNext(); if (!hasNext && lastSeenKey != null) { Token lastSeenToken = StorageService.getPartitioner().getToken(lastSeenKey.duplicate()); // let's check if we reached key upper bound already so we can skip one useless call to Cassandra if (maximumToken != getMinimumToken() && lastSeenToken.equals(maximumToken)) { return false; } List<Row> newKeys = getKeySlice(StorageService.getPartitioner().getToken(lastSeenKey), maximumToken, sliceQuery, pageSize, nowMillis); keys = getRowsIterator(newKeys, lastSeenKey); hasNext = keys.hasNext(); } return hasNext; }
NamesQueryFilter nqf = new NamesQueryFilter(names); SliceByNamesReadCommand cmd = new SliceByNamesReadCommand(ks, ByteBufferUtil.zeroByteBuffer(1), cf, 1L, nqf); StorageProxy.read(ImmutableList.<ReadCommand> of(cmd), ConsistencyLevel.QUORUM); log.info("Read on CF {} in KS {} succeeded", cf, ks); return;
public RowIterator(KeyRangeQuery keyRangeQuery, int pageSize, StoreTransaction txh) throws BackendException { this(StorageService.getPartitioner().getToken(keyRangeQuery.getKeyStart().asByteBuffer()), StorageService.getPartitioner().getToken(keyRangeQuery.getKeyEnd().asByteBuffer()), keyRangeQuery, pageSize, txh); }
@Override public KeyIterator getKeys(KeyRangeQuery keyRangeQuery, StoreTransaction txh) throws BackendException { IPartitioner partitioner = StorageService.getPartitioner(); // see rant about this in Astyanax implementation if (partitioner instanceof RandomPartitioner || partitioner instanceof Murmur3Partitioner) throw new PermanentBackendException("This operation is only supported when byte-ordered partitioner is used."); return new RowIterator(keyRangeQuery, storeManager.getPageSize(), txh); }
private static Token getMinimumToken() throws PermanentBackendException { IPartitioner partitioner = StorageService.getPartitioner(); if (partitioner instanceof RandomPartitioner) { return ((RandomPartitioner) partitioner).getMinimumToken(); } else if (partitioner instanceof Murmur3Partitioner) { return ((Murmur3Partitioner) partitioner).getMinimumToken(); } else if (partitioner instanceof ByteOrderedPartitioner) { //TODO: This makes the assumption that its an EdgeStore (i.e. 8 byte keys) return new BytesToken(com.thinkaurelius.titan.diskstorage.util.ByteBufferUtil.zeroByteBuffer(8)); } else { throw new PermanentBackendException("Unsupported partitioner: " + partitioner); } }
private static Token getMaximumToken() throws PermanentBackendException { IPartitioner partitioner = StorageService.getPartitioner(); if (partitioner instanceof RandomPartitioner) { return new BigIntegerToken(RandomPartitioner.MAXIMUM); } else if (partitioner instanceof Murmur3Partitioner) { return new LongToken(Murmur3Partitioner.MAXIMUM); } else if (partitioner instanceof ByteOrderedPartitioner) { //TODO: This makes the assumption that its an EdgeStore (i.e. 8 byte keys) return new BytesToken(com.thinkaurelius.titan.diskstorage.util.ByteBufferUtil.oneByteBuffer(8)); } else { throw new PermanentBackendException("Unsupported partitioner: " + partitioner); } } }