Source:
public void createDatastream(Datastream datastream) { PollUtils.poll(() -> { CreateIdRequest<String, Datastream> request = _builders.create().input(datastream).build(); ResponseFuture<IdResponse<String>> datastreamResponseFuture = _restClient.sendRequest(request); try { return datastreamResponseFuture.getResponse(); } catch (RemoteInvocationException e) { if (ExceptionUtils.getRootCause(e) instanceof TimeoutException) { LOG.warn("Timeout: createDatastream. May retry...", e); return null; } if (e instanceof RestLiResponseException) { int errorCode = ((RestLiResponseException) e).getStatus(); if (errorCode == HttpStatus.S_409_CONFLICT.getCode()) { Datastream existingDatastream = getDatastream(datastream.getName()); if (existingDatastream.getMetadata().get(DatastreamMetadataConstants.OWNER_KEY) .equals(datastream.getMetadata().get(DatastreamMetadataConstants.OWNER_KEY)) && existingDatastream.getMetadata().containsKey(DatastreamMetadataConstants.CREATION_MS) && Math.abs(System.currentTimeMillis() - Long.parseLong( existingDatastream.getMetadata().get(DatastreamMetadataConstants.CREATION_MS))) < getRetryTimeoutMs()) { return existingDatastream; } String msg = String.format("Datastream %s already exists", datastream.getName()); LOG.warn(msg, e); throw new DatastreamAlreadyExistsException(msg); } else if (errorCode == HttpStatus.S_403_FORBIDDEN.getCode()) { String msg = "Client is not authorized to invoke Datastream-CREATE, stream=" + datastream.getName(); ErrorLogger.logAndThrowDatastreamRuntimeException(LOG, msg, e); } } String errorMessage = String.format("Create Datastream %s failed with error.", datastream); ErrorLogger.logAndThrowDatastreamRuntimeException(LOG, errorMessage, e); return null; } }, Objects::nonNull, getRetryPeriodMs(), getRetryTimeoutMs()).orElseThrow(RetriesExhaustedExeption::new); }

Target:
public void createDatastream(Datastream datastream) { PollUtils.poll(() -> { if (!datastream.hasMetadata()) { datastream.setMetadata(new StringMap()); } String creationUid = UUID.randomUUID().toString(); datastream.getMetadata().put(DATASTREAM_UUID, creationUid); CreateIdRequest<String, Datastream> request = _builders.create().input(datastream).build(); ResponseFuture<IdResponse<String>> datastreamResponseFuture = _restClient.sendRequest(request); try { return datastreamResponseFuture.getResponse(); } catch (RemoteInvocationException e) { if (ExceptionUtils.getRootCause(e) instanceof TimeoutException) { LOG.warn("Timeout: createDatastream. May retry...", e); return null; } if (e instanceof RestLiResponseException) { int errorCode = ((RestLiResponseException) e).getStatus(); if (errorCode == HttpStatus.S_409_CONFLICT.getCode()) { Datastream existingDatastream = getDatastream(datastream.getName()); Optional<String> existingUid = Optional.ofNullable(existingDatastream.getMetadata().get(DATASTREAM_UUID)); if (existingUid.isPresent() && existingUid.get().equals(creationUid)) { return existingDatastream; } String msg = String.format("Datastream %s already exists", datastream.getName()); LOG.warn(msg, e); throw new DatastreamAlreadyExistsException(msg); } else if (errorCode == HttpStatus.S_403_FORBIDDEN.getCode()) { String msg = "Client is not authorized to invoke Datastream-CREATE, stream=" + datastream.getName(); ErrorLogger.logAndThrowDatastreamRuntimeException(LOG, msg, e); } } String errorMessage = String.format("Create Datastream %s failed with error.", datastream); ErrorLogger.logAndThrowDatastreamRuntimeException(LOG, errorMessage, e); return null; } }, Objects::nonNull, getRetryPeriodMs(), getRetryTimeoutMs()).orElseThrow(RetriesExhaustedExeption::new); }