Example #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!")
Example #2
0
 async def _collect_states_to_delete(self):
     states_to_delete = []
     for redis_dict in self._redis_dicts:
         for key in redis_dict.garbage_keys():
             state_scope = redis_dict.state_scope
             device_id = make_scoped_device_id(key, state_scope)
             sid = StateID(deviceID=device_id, type=redis_dict.redis_type)
             states_to_delete.append(sid)
     if len(states_to_delete) == 0:
         logging.debug("Not garbage collecting state. No state to delete!")
         return None
     # NetworkID will be filled in by Orchestrator from GW context
     return DeleteStatesRequest(networkID="", ids=states_to_delete)