예제 #1
0
    async def _resync(self):
        states_to_sync = []
        for redis_dict in self._redis_dicts:
            for key in redis_dict:
                version = redis_dict.get_version(key)
                device_id = make_scoped_device_id(key, redis_dict.state_scope)
                state_id = StateID(type=redis_dict.redis_type,
                                   deviceID=device_id)
                id_and_version = IDAndVersion(id=state_id, version=version)
                states_to_sync.append(id_and_version)

        if len(states_to_sync) == 0:
            logging.debug("Not re-syncing state. No local state found.")
            return
        state_client = self._grpc_client_manager.get_client()
        request = SyncStatesRequest(states=states_to_sync)
        response = await grpc_async_wrapper(
            state_client.SyncStates.future(
                request,
                DEFAULT_GRPC_TIMEOUT,
            ), self._loop)
        unsynced_states = set()
        for id_and_version in response.unsyncedStates:
            unsynced_states.add(
                (id_and_version.id.type, id_and_version.id.deviceID))
        # Update in-memory map to add already synced states
        for state in request.states:
            in_mem_key = make_mem_key(state.id.deviceID, state.id.type)
            if (state.id.type, state.id.deviceID) not in unsynced_states:
                self._state_versions[in_mem_key] = state.version

        self._has_resync_completed = True
        logging.info("Successfully resynced state with Orchestrator!")
예제 #2
0
 def SyncStates(self, request, context):
     unsynced_states = []
     for state in request.states:
         if state.id.type == LOG_TYPE:
             raise grpc.RpcError("Test Exception")
         elif state.id.type == NID_TYPE:
             id_and_version = IDAndVersion(id=state.id,
                                           version=state.version)
             unsynced_states.append(id_and_version)
     return SyncStatesResponse(unsyncedStates=unsynced_states, )