def test_create_function(self): ''' This test creates a new function and checks that it is persisted in the KVS in the expected format. ''' # Create a new function message and add it to our socket. def func(_, x): x + 1 function = Function() function.name = 'function' function.body = serializer.dump(func) self.socket.inbox.append(function.SerializeToString()) # Call the function creation script. create_function(self.socket, self.kvs_client, consistency=NORMAL) # Check that the function was created correctly. kvs_name = sutils.get_func_kvs_name(function.name) result = self.kvs_client.get(kvs_name) created = result[kvs_name] self.assertTrue(created is not None) self.assertEqual(type(created), LWWPairLattice) created = serializer.load_lattice(created) self.assertEqual(func('', 1), created('', 1))
def test_create_causal_function(self): ''' This test creates a new function and checks that it is persisted in the KVS in the expected format in causal mode. ''' # Create a new function message and add it to our socket. def func(_, x): x + 1 function = Function() function.name = 'function' function.body = serializer.dump(func) self.socket.inbox.append(function.SerializeToString()) # Call the function creation script. create_function(self.socket, self.kvs_client, consistency=MULTI) # Check that the function was created correctly. kvs_name = sutils.get_func_kvs_name(function.name) result = self.kvs_client.get(kvs_name) created = result[kvs_name] self.assertTrue(created is not None) self.assertEqual(type(created), SingleKeyCausalLattice) self.assertEqual(created.vector_clock, sutils.DEFAULT_VC) self.assertEqual(len(created.reveal()), 1) created = serializer.load_lattice(created)[0] self.assertEqual(func('', 1), created('', 1))
def retrieve_function(name, kvs, user_library, consistency=NORMAL): kvs_name = sutils.get_func_kvs_name(name) if consistency == NORMAL: # This means that the function is stored in an LWWPairLattice. lattice = kvs.get(kvs_name)[kvs_name] if lattice: result = serializer.load_lattice(lattice) else: return None else: # This means that the function is stored in an SingleKeyCausalLattice. _, result = kvs.causal_get([kvs_name]) lattice = result[kvs_name] if lattice: # If there are multiple concurrent values, we arbitrarily pick the # first one listed. result = serializer.load_lattice(lattice)[0] else: return None # Check to see if the result is a tuple. This means that the first object # in the tuple is a class that we can initialize, and the second value is a # set of initialization args. Otherwise, we just return the retrieved # function. if type(result) == tuple: cls = result[0] args = (user_library, ) + result[1] obj = cls(*args) result = obj.run return result
def create_function(func_create_socket, kvs, consistency=NORMAL): func = Function() func.ParseFromString(func_create_socket.recv()) name = sutils.get_func_kvs_name(func.name) logging.info('Creating function %s.' % (name)) if consistency == NORMAL: body = LWWPairLattice(sutils.generate_timestamp(0), func.body) kvs.put(name, body) else: skcl = SingleKeyCausalLattice(sutils.DEFAULT_VC, SetLattice({func.body})) kvs.put(name, skcl) funcs = utils.get_func_list(kvs, '', fullname=True) funcs.append(name) utils.put_func_list(kvs, funcs) func_create_socket.send(sutils.ok_resp)
def create_function(function, kvs_client, fname='func', ltype=LWWPairLattice): serialized = serializer.dump_lattice(function, ltype) kvs_client.put(get_func_kvs_name(fname), serialized)