Java Code Examples for org.elasticsearch.common.UUIDs#randomBase64UUID()

The following examples show how to use org.elasticsearch.common.UUIDs#randomBase64UUID() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlobStoreRepository.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public String startVerification() {
    try {
        if (isReadOnly()) {
            // It's readonly - so there is not much we can do here to verify it apart from reading the blob store metadata
            latestIndexBlobId();
            return "read-only";
        } else {
            String seed = UUIDs.randomBase64UUID();
            byte[] testBytes = seed.getBytes(StandardCharsets.UTF_8);
            BlobContainer testContainer = blobStore().blobContainer(basePath().add(testBlobPrefix(seed)));
            String blobName = "master.dat";
            BytesArray bytes = new BytesArray(testBytes);
            try (InputStream stream = bytes.streamInput()) {
                testContainer.writeBlobAtomic(blobName, stream, bytes.length(), true);
            }
            return seed;
        }
    } catch (IOException exp) {
        throw new RepositoryVerificationException(metadata.name(), "path " + basePath() + " is not accessible on master node", exp);
    }
}
 
Example 2
Source File: SysSnapshotsTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnavailableSnapshotsAreFilteredOut() {
    HashMap<String, SnapshotId> snapshots = new HashMap<>();
    SnapshotId s1 = new SnapshotId("s1", UUIDs.randomBase64UUID());
    SnapshotId s2 = new SnapshotId("s2", UUIDs.randomBase64UUID());
    snapshots.put(s1.getUUID(), s1);
    snapshots.put(s2.getUUID(), s2);
    RepositoryData repositoryData = new RepositoryData(
        1, snapshots, Collections.emptyMap(), Collections.emptyMap(), ShardGenerations.EMPTY);

    Repository r1 = mock(Repository.class);
    when(r1.getRepositoryData()).thenReturn(repositoryData);
    when(r1.getMetadata()).thenReturn(new RepositoryMetaData("repo1", "fs", Settings.EMPTY));
    when(r1.getSnapshotInfo(eq(s1))).thenThrow(new SnapshotException("repo1", "s1", "Everything is wrong"));
    when(r1.getSnapshotInfo(eq(s2))).thenReturn(new SnapshotInfo(s2, Collections.emptyList(), SnapshotState.SUCCESS));

    SysSnapshots sysSnapshots = new SysSnapshots(() -> Collections.singletonList(r1));
    List<SysSnapshot> currentSnapshots = ImmutableList.copyOf(sysSnapshots.currentSnapshots());
    assertThat(
        currentSnapshots.stream().map(SysSnapshot::name).collect(Collectors.toList()),
        containsInAnyOrder("s1", "s2")
    );
}
 
Example 3
Source File: QueryTester.java    From crate with Apache License 2.0 6 votes vote down vote up
void indexValue(String column, Object value) throws IOException {
    DocumentMapper mapper = indexEnv.mapperService().documentMapperSafe();
    InsertSourceGen sourceGen = InsertSourceGen.of(
        CoordinatorTxnCtx.systemTransactionContext(),
        sqlExecutor.functions(),
        table,
        table.concreteIndices()[0],
        GeneratedColumns.Validation.NONE,
        Collections.singletonList(table.getReference(ColumnIdent.fromPath(column)))
    );
    BytesReference source = sourceGen.generateSourceAndCheckConstraintsAsBytesReference(new Object[]{value});
    SourceToParse sourceToParse = new SourceToParse(
        table.concreteIndices()[0],
        UUIDs.randomBase64UUID(),
        source,
        XContentType.JSON
    );
    ParsedDocument parsedDocument = mapper.parse(sourceToParse);
    indexEnv.writer().addDocuments(parsedDocument.docs());
}
 
Example 4
Source File: MockTransportService.java    From crate with Apache License 2.0 6 votes vote down vote up
public static MockTransportService createNewService(Settings settings,
                                                    Transport transport,
                                                    Version version,
                                                    ThreadPool threadPool,
                                                    @Nullable ClusterSettings clusterSettings) {
    return new MockTransportService(
        settings,
        transport,
        threadPool,
        TransportService.NOOP_TRANSPORT_INTERCEPTOR,
        boundAddress -> new DiscoveryNode(
            Node.NODE_NAME_SETTING.get(settings),
            UUIDs.randomBase64UUID(),
            boundAddress.publishAddress(),
            Node.NODE_ATTRIBUTES.getAsMap(settings),
            DiscoveryNode.getRolesFromSettings(settings),
            version
        ),
        clusterSettings
    );
}
 
Example 5
Source File: TransportShardDeleteActionTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Before
public void prepare() throws Exception {
    indexUUID = UUIDs.randomBase64UUID();
    IndicesService indicesService = mock(IndicesService.class);
    IndexService indexService = mock(IndexService.class);
    when(indicesService.indexServiceSafe(new Index(TABLE_IDENT.indexNameOrAlias(), indexUUID))).thenReturn(indexService);
    indexShard = mock(IndexShard.class);
    when(indexService.getShard(0)).thenReturn(indexShard);


    transportShardDeleteAction = new TransportShardDeleteAction(
        MockTransportService.createNewService(
            Settings.EMPTY, Version.CURRENT, THREAD_POOL, clusterService.getClusterSettings()),
        mock(IndexNameExpressionResolver.class),
        mock(ClusterService.class),
        indicesService,
        mock(ThreadPool.class),
        mock(ShardStateAction.class),
        mock(SchemaUpdateClient.class)
    );
}
 
Example 6
Source File: ShardDeleteRequestTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreaming() throws Exception {
    ShardId shardId = new ShardId("test", UUIDs.randomBase64UUID(), 1);
    UUID jobId = UUID.randomUUID();
    ShardDeleteRequest request = new ShardDeleteRequest(shardId, jobId);

    request.add(123, new ShardDeleteRequest.Item("99"));
    request.add(5, new ShardDeleteRequest.Item("42"));

    BytesStreamOutput out = new BytesStreamOutput();
    request.writeTo(out);

    StreamInput in = out.bytes().streamInput();
    ShardDeleteRequest request2 = new ShardDeleteRequest(in);

    assertThat(request, equalTo(request2));
}
 
Example 7
Source File: Store.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Marks this store as corrupted. This method writes a {@code corrupted_${uuid}} file containing the given exception
 * message. If a store contains a {@code corrupted_${uuid}} file {@link #isMarkedCorrupted()} will return <code>true</code>.
 */
public void markStoreCorrupted(IOException exception) throws IOException {
    ensureOpen();
    if (!isMarkedCorrupted()) {
        String uuid = CORRUPTED + UUIDs.randomBase64UUID();
        try (IndexOutput output = this.directory().createOutput(uuid, IOContext.DEFAULT)) {
            CodecUtil.writeHeader(output, CODEC, VERSION);
            BytesStreamOutput out = new BytesStreamOutput();
            out.writeException(exception);
            BytesReference bytes = out.bytes();
            output.writeVInt(bytes.length());
            BytesRef ref = bytes.toBytesRef();
            output.writeBytes(ref.bytes, ref.offset, ref.length);
            CodecUtil.writeFooter(output);
        } catch (IOException ex) {
            logger.warn("Can't mark store as corrupted", ex);
        }
        directory().sync(Collections.singleton(uuid));
    }
}
 
Example 8
Source File: RecoveryTarget.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new recovery target object that represents a recovery to the provided shard.
 *
 * @param indexShard                        local shard where we want to recover to
 * @param sourceNode                        source node of the recovery where we recover from
 * @param listener                          called when recovery is completed/failed
 * @param ensureClusterStateVersionCallback callback to ensure that the current node is at least on a cluster state with the provided
 *                                          version; necessary for primary relocation so that new primary knows about all other ongoing
 *                                          replica recoveries when replicating documents (see {@link RecoverySourceHandler})
 */
public RecoveryTarget(final IndexShard indexShard,
               final DiscoveryNode sourceNode,
               final PeerRecoveryTargetService.RecoveryListener listener,
               final LongConsumer ensureClusterStateVersionCallback) {
    super("recovery_status");
    this.cancellableThreads = new CancellableThreads();
    this.recoveryId = ID_GENERATOR.incrementAndGet();
    this.listener = listener;
    this.logger = Loggers.getLogger(getClass(), indexShard.shardId());
    this.indexShard = indexShard;
    this.sourceNode = sourceNode;
    this.shardId = indexShard.shardId();
    this.tempFilePrefix = RECOVERY_PREFIX + UUIDs.randomBase64UUID() + ".";
    this.store = indexShard.store();
    this.ensureClusterStateVersionCallback = ensureClusterStateVersionCallback;
    // make sure the store is not released until we are done.
    store.incRef();
    indexShard.recoveryStats().incCurrentAsTarget();
}
 
Example 9
Source File: Translog.java    From crate with Apache License 2.0 5 votes vote down vote up
static String createEmptyTranslog(Path location, long initialGlobalCheckpoint, ShardId shardId,
                                  ChannelFactory channelFactory, long primaryTerm) throws IOException {
    IOUtils.rm(location);
    Files.createDirectories(location);
    final Checkpoint checkpoint = Checkpoint.emptyTranslogCheckpoint(0, 1, initialGlobalCheckpoint, 1);
    final Path checkpointFile = location.resolve(CHECKPOINT_FILE_NAME);
    Checkpoint.write(channelFactory, checkpointFile, checkpoint, StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);
    IOUtils.fsync(checkpointFile, false);
    final String translogUUID = UUIDs.randomBase64UUID();
    TranslogWriter writer = TranslogWriter.create(
        shardId,
        translogUUID,
        1,
        location.resolve(getFilename(1)),
        channelFactory,
        new ByteSizeValue(10),
        1,
        initialGlobalCheckpoint,
        () -> {
            throw new UnsupportedOperationException();
        },
        () -> {
            throw new UnsupportedOperationException();
        },
        primaryTerm,
        new TragicExceptionHolder(),
        seqNo -> {
            throw new UnsupportedOperationException();
        }
    );
    writer.close();
    return translogUUID;
}
 
Example 10
Source File: RepositoryDataTests.java    From crate with Apache License 2.0 5 votes vote down vote up
public void testGetSnapshotState() {
    final SnapshotId snapshotId = new SnapshotId(randomAlphaOfLength(8), UUIDs.randomBase64UUID());
    final SnapshotState state = randomFrom(SnapshotState.values());
    final RepositoryData repositoryData = RepositoryData.EMPTY.addSnapshot(snapshotId, state, ShardGenerations.EMPTY);
    assertEquals(state, repositoryData.getSnapshotState(snapshotId));
    assertNull(repositoryData.getSnapshotState(new SnapshotId(randomAlphaOfLength(8), UUIDs.randomBase64UUID())));
}
 
Example 11
Source File: RepositoryData.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Resolve the given index names to index ids, creating new index ids for
 * new indices in the repository.
 */
public List<IndexId> resolveNewIndices(final List<String> indicesToResolve) {
    List<IndexId> snapshotIndices = new ArrayList<>();
    for (String index : indicesToResolve) {
        final IndexId indexId;
        if (indices.containsKey(index)) {
            indexId = indices.get(index);
        } else {
            indexId = new IndexId(index, UUIDs.randomBase64UUID());
        }
        snapshotIndices.add(indexId);
    }
    return snapshotIndices;
}
 
Example 12
Source File: CoordinatorTests.java    From crate with Apache License 2.0 5 votes vote down vote up
ClusterNode restartedNode(Function<MetaData, MetaData> adaptGlobalMetaData, Function<Long, Long> adaptCurrentTerm,
                          Settings nodeSettings) {
    final TransportAddress address = randomBoolean() ? buildNewFakeTransportAddress() : localNode.getAddress();
    final DiscoveryNode newLocalNode = new DiscoveryNode(localNode.getName(), localNode.getId(),
        UUIDs.randomBase64UUID(random()), // generated deterministically for repeatable tests
        address.address().getHostString(), address.getAddress(), address, Collections.emptyMap(),
        localNode.isMasterNode() ? EnumSet.allOf(Role.class) : emptySet(), Version.CURRENT);
    return new ClusterNode(nodeIndex, newLocalNode,
        node -> new MockPersistedState(newLocalNode, persistedState, adaptGlobalMetaData, adaptCurrentTerm), nodeSettings);
}
 
Example 13
Source File: ClusterState.java    From crate with Apache License 2.0 4 votes vote down vote up
public ClusterState build() {
    if (UNKNOWN_UUID.equals(uuid)) {
        uuid = UUIDs.randomBase64UUID();
    }
    return new ClusterState(clusterName, version, uuid, metaData, routingTable, nodes, blocks, customs.build(), fromDiff);
}
 
Example 14
Source File: AllocationId.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new allocation id for initializing allocation.
 */
public static AllocationId newInitializing() {
    return new AllocationId(UUIDs.randomBase64UUID(), null);
}
 
Example 15
Source File: MetaDataIndexTemplateService.java    From crate with Apache License 2.0 4 votes vote down vote up
private static void validateAndAddTemplate(final PutRequest request, IndexTemplateMetaData.Builder templateBuilder,
        IndicesService indicesService, NamedXContentRegistry xContentRegistry) throws Exception {
    Index createdIndex = null;
    final String temporaryIndexName = UUIDs.randomBase64UUID();
    try {
        // use the provided values, otherwise just pick valid dummy values
        int dummyPartitionSize = IndexMetaData.INDEX_ROUTING_PARTITION_SIZE_SETTING.get(request.settings);
        int dummyShards = request.settings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS,
                dummyPartitionSize == 1 ? 1 : dummyPartitionSize + 1);

        //create index service for parsing and validating "mappings"
        Settings dummySettings = Settings.builder()
            .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
            .put(request.settings)
            .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, dummyShards)
            .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
            .put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID())
            .build();

        final IndexMetaData tmpIndexMetadata = IndexMetaData.builder(temporaryIndexName).settings(dummySettings).build();
        IndexService dummyIndexService = indicesService.createIndex(tmpIndexMetadata, Collections.emptyList());
        createdIndex = dummyIndexService.index();

        templateBuilder.order(request.order);
        templateBuilder.version(request.version);
        templateBuilder.patterns(request.indexPatterns);
        templateBuilder.settings(request.settings);

        Map<String, Map<String, Object>> mappingsForValidation = new HashMap<>();
        for (Map.Entry<String, String> entry : request.mappings.entrySet()) {
            try {
                templateBuilder.putMapping(entry.getKey(), entry.getValue());
            } catch (Exception e) {
                throw new MapperParsingException("Failed to parse mapping [{}]: {}", e, entry.getKey(), e.getMessage());
            }
            mappingsForValidation.put(entry.getKey(), MapperService.parseMapping(xContentRegistry, entry.getValue()));
        }

        dummyIndexService.mapperService().merge(mappingsForValidation, MergeReason.MAPPING_UPDATE, false);

    } finally {
        if (createdIndex != null) {
            indicesService.removeIndex(createdIndex, NO_LONGER_ASSIGNED, " created for parsing template mapping");
        }
    }
}
 
Example 16
Source File: MetaData.java    From crate with Apache License 2.0 4 votes vote down vote up
public Builder generateClusterUuidIfNeeded() {
    if (clusterUUID.equals(UNKNOWN_CLUSTER_UUID)) {
        clusterUUID = UUIDs.randomBase64UUID();
    }
    return this;
}
 
Example 17
Source File: FsBlobContainer.java    From crate with Apache License 2.0 4 votes vote down vote up
public static String tempBlobName(final String blobName) {
    return "pending-" + blobName + "-" + UUIDs.randomBase64UUID();
}
 
Example 18
Source File: NodeEnvironment.java    From crate with Apache License 2.0 4 votes vote down vote up
public static String generateNodeId(Settings settings) {
    Random random = Randomness.get(settings, NODE_ID_SEED_SETTING);
    return UUIDs.randomBase64UUID(random);
}
 
Example 19
Source File: SysShardsExpressionsTest.java    From crate with Apache License 2.0 4 votes vote down vote up
private IndexShard mockIndexShard() {
    IndexService indexService = mock(IndexService.class);
    indexUUID = UUIDs.randomBase64UUID();
    Index index = new Index(indexName, indexUUID);
    ShardId shardId = new ShardId(indexName, indexUUID, 1);

    IndexShard indexShard = mock(IndexShard.class);
    when(indexService.index()).thenReturn(index);
    when(indexShard.shardId()).thenReturn(shardId);
    when(indexShard.state()).thenReturn(IndexShardState.STARTED);

    StoreStats storeStats = new StoreStats(123456L);
    when(indexShard.storeStats()).thenReturn(storeStats);

    Path dataPath = Paths.get("/dummy/" + indexUUID + "/" + shardId.id());
    when(indexShard.shardPath()).thenReturn(new ShardPath(false, dataPath, dataPath, shardId));

    DocsStats docsStats = new DocsStats(654321L, 0L, 200L);
    when(indexShard.docStats()).thenReturn(docsStats).thenThrow(IllegalIndexShardStateException.class);

    ShardRouting shardRouting = ShardRouting.newUnassigned(
        shardId, true, RecoverySource.PeerRecoverySource.INSTANCE, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "foo"));
    shardRouting = ShardRoutingHelper.initialize(shardRouting, "node1");
    shardRouting = ShardRoutingHelper.moveToStarted(shardRouting);
    shardRouting = ShardRoutingHelper.relocate(shardRouting, "node_X");
    when(indexShard.routingEntry()).thenReturn(shardRouting);
    when(indexShard.minimumCompatibleVersion()).thenReturn(Version.LATEST);

    RecoveryState recoveryState = mock(RecoveryState.class);
    when(indexShard.recoveryState()).thenReturn(recoveryState);
    RecoveryState.Index recoveryStateIndex = mock(RecoveryState.Index.class);
    RecoveryState.Timer recoveryStateTimer = mock(RecoveryState.Timer.class);
    when(recoveryState.getRecoverySource()).thenReturn(RecoverySource.PeerRecoverySource.INSTANCE);
    when(recoveryState.getIndex()).thenReturn(recoveryStateIndex);
    when(recoveryState.getStage()).thenReturn(RecoveryState.Stage.DONE);
    when(recoveryState.getTimer()).thenReturn(recoveryStateTimer);

    when(recoveryStateIndex.totalBytes()).thenReturn(2048L);
    when(recoveryStateIndex.reusedBytes()).thenReturn(1024L);
    when(recoveryStateIndex.recoveredBytes()).thenReturn(1024L);
    when(recoveryStateIndex.totalFileCount()).thenReturn(2);
    when(recoveryStateIndex.reusedFileCount()).thenReturn(1);
    when(recoveryStateIndex.recoveredFileCount()).thenReturn(1);
    when(recoveryStateTimer.time()).thenReturn(10000L);

    return indexShard;
}
 
Example 20
Source File: DiscoveryNode.java    From crate with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new {@link DiscoveryNode}
 * <p>
 * <b>Note:</b> if the version of the node is unknown {@link Version#minimumCompatibilityVersion()} should be used for the current
 * version. it corresponds to the minimum version this elasticsearch version can communicate with. If a higher version is used
 * the node might not be able to communicate with the remove node. After initial handshakes node versions will be discovered
 * and updated.
 * </p>
 *
 * @param nodeName         the nodes name
 * @param nodeId           the nodes unique persistent id. An ephemeral id will be auto generated.
 * @param address          the nodes transport address
 * @param attributes       node attributes
 * @param roles            node roles
 * @param version          the version of the node
 */
public DiscoveryNode(String nodeName, String nodeId, TransportAddress address,
                     Map<String, String> attributes, Set<Role> roles, Version version) {
    this(nodeName, nodeId, UUIDs.randomBase64UUID(), address.address().getHostString(), address.getAddress(), address, attributes,
        roles, version);
}