Search in sources :

Example 1 with BrowserHeaders

use of io.deephaven.javascript.proto.dhinternal.browserheaders.BrowserHeaders in project deephaven-core by deephaven.

the class WorkerConnection method connectToWorker.

/**
 * Creates a new session based on the current auth info, and attempts to re-create all tables and other objects that
 * were currently open.
 *
 * First we assume that the auth token provider is valid, and ask for a new token to provide to the worker.
 *
 * Given that token, we create a new session on the worker server.
 *
 * When a table is first fetched, it might fail - the worker connection will keep trying to connect even if the
 * failure is in one of the above steps. A later attempt to fetch that table may succeed however.
 *
 * Once the table has been successfully fetched, after each reconnect until the table is close()d we'll attempt to
 * restore the table by re-fetching the table, then reapplying all operations on it.
 */
private void connectToWorker(Supplier<Promise<ConnectToken>> authTokenPromiseSupplier) {
    info.running().then(queryWorkerRunning -> {
        // get the auth token
        return authTokenPromiseSupplier.get();
    }).then(authToken -> {
        // create a new session
        HandshakeRequest handshakeRequest = new HandshakeRequest();
        if (authToken != null) {
            Uint8Array token = new Uint8Array(authToken.getBytes().length);
            handshakeRequest.setPayload(token);
        }
        handshakeRequest.setAuthProtocol(1);
        return Callbacks.<HandshakeResponse, Object>grpcUnaryPromise(c -> sessionServiceClient.newSession(handshakeRequest, (BrowserHeaders) null, c::apply));
    }).then(handshakeResponse -> {
        // start the reauth cycle
        authUpdate(handshakeResponse);
        // subscribe to fatal errors
        subscribeToTerminationNotification();
        state = State.Connected;
        JsLog.debug("Connected to worker, ensuring all states are refreshed");
        // mark that we succeeded
        newSessionReconnect.success();
        // nuke pending callbacks, we'll remake them
        handleCallbacks = new JsWeakMap<>();
        definitionCallbacks = new JsWeakMap<>();
        // for each cts in the cache, get all with active subs
        ClientTableState[] hasActiveSubs = cache.getAllStates().stream().peek(cts -> {
            cts.getHandle().setConnected(false);
            cts.setSubscribed(false);
            cts.forActiveLifecycles(item -> {
                assert !(item instanceof JsTable) || ((JsTable) item).state() == cts : "Invalid table state " + item + " does not point to state " + cts;
                item.suppressEvents();
            });
        }).filter(cts -> !cts.isEmpty()).peek(cts -> {
            cts.forActiveTables(t -> {
                assert t.state().isAncestor(cts) : "Invalid binding " + t + " (" + t.state() + ") does not contain " + cts;
            });
        }).toArray(ClientTableState[]::new);
        // clear caches
        List<ClientTableState> inactiveStatesToRemove = cache.getAllStates().stream().filter(ClientTableState::isEmpty).collect(Collectors.toList());
        inactiveStatesToRemove.forEach(cache::release);
        flushable.clear();
        reviver.revive(metadata, hasActiveSubs);
        tableMaps.forEach((handle, tableMap) -> tableMap.refetch());
        figures.forEach((p0, p1, p2) -> p0.refetch());
        info.connected();
        // if any tables have been requested, make sure they start working now that we are connected
        onOpen.forEach(c -> c.onSuccess(null));
        onOpen.clear();
        // // start a heartbeat to check if connection is properly alive
        // ping(success.getAuthSessionToken());
        startExportNotificationsStream();
        return Promise.resolve(handshakeResponse);
    }, fail -> {
        // this is non-recoverable, connection/auth/registration failed, but we'll let it start again when
        // state changes
        state = State.Failed;
        JsLog.debug("Failed to connect to worker.");
        final String failure = fail.toString();
        // notify all pending fetches that they failed
        onOpen.forEach(c -> c.onFailure(failure));
        onOpen.clear();
        // if (server != null) {
        // // explicitly disconnect from the query worker
        // server.close();
        // }
        // signal that we should try again
        newSessionReconnect.failed();
        // inform the UI that it failed to connect
        info.failureHandled("Failed to connect: " + failure);
        return null;
    });
}
Also used : Arrays(java.util.Arrays) InitialTableDefinition(io.deephaven.web.client.api.barrage.def.InitialTableDefinition) LogSubscriptionRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.console_pb.LogSubscriptionRequest) JsPropertyMap(jsinterop.base.JsPropertyMap) Buffer(io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.schema_generated.org.apache.arrow.flatbuf.Buffer) HandshakeResponse(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.session_pb.HandshakeResponse) TypedTicket(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.ticket_pb.TypedTicket) JsTimeZone(io.deephaven.web.client.api.i18n.JsTimeZone) ObjectServiceClient(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.object_pb_service.ObjectServiceClient) StackTrace(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.session_pb.terminationnotificationresponse.StackTrace) io.deephaven.web.shared.data(io.deephaven.web.shared.data) Map(java.util.Map) SessionServiceClient(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.session_pb_service.SessionServiceClient) FieldsChangeUpdate(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.application_pb.FieldsChangeUpdate) ListFieldsRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.application_pb.ListFieldsRequest) Set(java.util.Set) ClientTableState(io.deephaven.web.client.state.ClientTableState) ExportedTableCreationResponse(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.ExportedTableCreationResponse) JsArray(elemental2.core.JsArray) JsSet(elemental2.core.JsSet) Uint8Array(elemental2.core.Uint8Array) MetadataVersion(io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.schema_generated.org.apache.arrow.flatbuf.MetadataVersion) JsVariableDefinition(io.deephaven.web.client.api.console.JsVariableDefinition) KeyValue(io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.schema_generated.org.apache.arrow.flatbuf.KeyValue) ConsoleServiceClient(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.console_pb_service.ConsoleServiceClient) BarrageMessageWrapper(io.deephaven.javascript.proto.dhinternal.io.deephaven.barrage.flatbuf.barrage_generated.io.deephaven.barrage.flatbuf.BarrageMessageWrapper) JsRunnable(io.deephaven.web.shared.fu.JsRunnable) JsItr(io.deephaven.web.client.fu.JsItr) JsOptional(jsinterop.annotations.JsOptional) JsWidget(io.deephaven.web.client.api.widget.JsWidget) JsFigure(io.deephaven.web.client.api.widget.plot.JsFigure) Promise(elemental2.promise.Promise) BiDiStream(io.deephaven.web.client.api.barrage.stream.BiDiStream) Supplier(java.util.function.Supplier) RequestBatcher(io.deephaven.web.client.api.batch.RequestBatcher) ArrayList(java.util.ArrayList) Builder(io.deephaven.javascript.proto.dhinternal.flatbuffers.Builder) ResponseStreamWrapper(io.deephaven.web.client.api.barrage.stream.ResponseStreamWrapper) BiConsumer(java.util.function.BiConsumer) TableReviver(io.deephaven.web.client.state.TableReviver) FetchObjectRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.object_pb.FetchObjectRequest) JsTreeTable(io.deephaven.web.client.api.tree.JsTreeTable) TableServiceClient(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb_service.TableServiceClient) LazyPromise(io.deephaven.web.client.fu.LazyPromise) Nullable(javax.annotation.Nullable) BrowserHeaders(io.deephaven.javascript.proto.dhinternal.browserheaders.BrowserHeaders) LogSubscriptionData(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.console_pb.LogSubscriptionData) Ticket(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.ticket_pb.Ticket) ColumnDefinition(io.deephaven.web.client.api.barrage.def.ColumnDefinition) RecordBatch(io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.message_generated.org.apache.arrow.flatbuf.RecordBatch) MergeTablesRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.MergeTablesRequest) ApplyPreviewColumnsRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.ApplyPreviewColumnsRequest) InputTableServiceClient(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.inputtable_pb_service.InputTableServiceClient) BarrageSubscriptionOptions(io.deephaven.javascript.proto.dhinternal.io.deephaven.barrage.flatbuf.barrage_generated.io.deephaven.barrage.flatbuf.BarrageSubscriptionOptions) ColumnConversionMode(io.deephaven.javascript.proto.dhinternal.io.deephaven.barrage.flatbuf.barrage_generated.io.deephaven.barrage.flatbuf.ColumnConversionMode) EmptyTableRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.EmptyTableRequest) BarrageUpdateMetadata(io.deephaven.javascript.proto.dhinternal.io.deephaven.barrage.flatbuf.barrage_generated.io.deephaven.barrage.flatbuf.BarrageUpdateMetadata) ByteBuffer(java.nio.ByteBuffer) TableConfig(io.deephaven.web.client.api.batch.TableConfig) TableReference(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.TableReference) JsVariableChanges(io.deephaven.web.client.api.console.JsVariableChanges) ReleaseRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.session_pb.ReleaseRequest) FlightData(io.deephaven.javascript.proto.dhinternal.arrow.flight.protocol.flight_pb.FlightData) ApplicationServiceClient(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.application_pb_service.ApplicationServiceClient) ExportedTableUpdatesRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.ExportedTableUpdatesRequest) JsLog(io.deephaven.web.client.fu.JsLog) Collectors(java.util.stream.Collectors) HasTableBinding(io.deephaven.web.client.state.HasTableBinding) List(java.util.List) FlightServiceClient(io.deephaven.javascript.proto.dhinternal.arrow.flight.protocol.flight_pb_service.FlightServiceClient) HasLifecycle(io.deephaven.web.client.api.lifecycle.HasLifecycle) Message(io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.message_generated.org.apache.arrow.flatbuf.Message) JsWeakMap(elemental2.core.JsWeakMap) BarrageSubscriptionRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.barrage.flatbuf.barrage_generated.io.deephaven.barrage.flatbuf.BarrageSubscriptionRequest) ExportedTableUpdateMessage(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.ExportedTableUpdateMessage) Optional(java.util.Optional) TimeTableRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.TimeTableRequest) HandshakeRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.session_pb.HandshakeRequest) Code(io.deephaven.javascript.proto.dhinternal.grpcweb.grpc.Code) Grpc(io.deephaven.javascript.proto.dhinternal.grpcweb.Grpc) BrowserFlightServiceClient(io.deephaven.javascript.proto.dhinternal.arrow.flight.protocol.browserflight_pb_service.BrowserFlightServiceClient) BarrageMessageType(io.deephaven.javascript.proto.dhinternal.io.deephaven.barrage.flatbuf.barrage_generated.io.deephaven.barrage.flatbuf.BarrageMessageType) JsMethod(jsinterop.annotations.JsMethod) HashMap(java.util.HashMap) StateCache(io.deephaven.web.client.api.state.StateCache) HashSet(java.util.HashSet) FetchTableRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.table_pb.FetchTableRequest) Js(jsinterop.base.Js) FieldNode(io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.message_generated.org.apache.arrow.flatbuf.FieldNode) Charset(java.nio.charset.Charset) Field(io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.schema_generated.org.apache.arrow.flatbuf.Field) FetchObjectResponse(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.object_pb.FetchObjectResponse) Long(io.deephaven.javascript.proto.dhinternal.flatbuffers.Long) JsConsumer(io.deephaven.web.shared.fu.JsConsumer) DomGlobal(elemental2.dom.DomGlobal) Schema(io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.schema_generated.org.apache.arrow.flatbuf.Schema) TerminationNotificationRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.session_pb.TerminationNotificationRequest) JsDataHandler(io.deephaven.web.client.api.parse.JsDataHandler) BarrageUtils(io.deephaven.web.client.api.barrage.BarrageUtils) FieldInfo(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.application_pb.FieldInfo) BitSet(java.util.BitSet) MessageHeader(io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.message_generated.org.apache.arrow.flatbuf.MessageHeader) Grpc(io.deephaven.javascript.proto.dhinternal.grpcweb.Grpc) ClientTableState(io.deephaven.web.client.state.ClientTableState) Uint8Array(elemental2.core.Uint8Array) HandshakeRequest(io.deephaven.javascript.proto.dhinternal.io.deephaven.proto.session_pb.HandshakeRequest)

Aggregations

JsArray (elemental2.core.JsArray)1 JsSet (elemental2.core.JsSet)1 JsWeakMap (elemental2.core.JsWeakMap)1 Uint8Array (elemental2.core.Uint8Array)1 DomGlobal (elemental2.dom.DomGlobal)1 Promise (elemental2.promise.Promise)1 FieldNode (io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.message_generated.org.apache.arrow.flatbuf.FieldNode)1 Message (io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.message_generated.org.apache.arrow.flatbuf.Message)1 MessageHeader (io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.message_generated.org.apache.arrow.flatbuf.MessageHeader)1 RecordBatch (io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.message_generated.org.apache.arrow.flatbuf.RecordBatch)1 Buffer (io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.schema_generated.org.apache.arrow.flatbuf.Buffer)1 Field (io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.schema_generated.org.apache.arrow.flatbuf.Field)1 KeyValue (io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.schema_generated.org.apache.arrow.flatbuf.KeyValue)1 MetadataVersion (io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.schema_generated.org.apache.arrow.flatbuf.MetadataVersion)1 Schema (io.deephaven.javascript.proto.dhinternal.arrow.flight.flatbuf.schema_generated.org.apache.arrow.flatbuf.Schema)1 BrowserFlightServiceClient (io.deephaven.javascript.proto.dhinternal.arrow.flight.protocol.browserflight_pb_service.BrowserFlightServiceClient)1 FlightData (io.deephaven.javascript.proto.dhinternal.arrow.flight.protocol.flight_pb.FlightData)1 FlightServiceClient (io.deephaven.javascript.proto.dhinternal.arrow.flight.protocol.flight_pb_service.FlightServiceClient)1 BrowserHeaders (io.deephaven.javascript.proto.dhinternal.browserheaders.BrowserHeaders)1 Builder (io.deephaven.javascript.proto.dhinternal.flatbuffers.Builder)1