8 #include <sandesh/request_pipeline.h>
10 #include <boost/algorithm/string/join.hpp>
11 #include <boost/algorithm/string/predicate.hpp>
12 #include <boost/foreach.hpp>
13 #include <boost/functional/hash.hpp>
14 #include <boost/ptr_container/ptr_map.hpp>
15 #include <boost/uuid/uuid.hpp>
33 #include "config_client_log_types.h"
34 #include "config_client_show_types.h"
35 #include "sandesh/common/vns_constants.h"
40 using std::unique_ptr;
49 "config_client::ObjectProcessor";
55 dbif_.reset(ConfigStaticObjectFactory::CreateRef<cass::cql::CqlIf>(
59 static_cast<bool>(
options.config_db_use_ssl),
options.config_db_ca_certs));
67 ConfigStaticObjectFactory::Create<ConfigCassandraPartition>
68 (
this,
static_cast<size_t>(i)));
89 if (!
dbif_->Db_Init()) {
91 "Database initialization failed");
96 "Cassandra SM: Db SetTableSpace");
97 if (!
dbif_->Db_SetTablespace(
98 g_vns_constants.API_SERVER_KEYSPACE_NAME)) {
100 "Setting database keyspace failed");
105 "Cassandra SM: Db UseColumnFamily uuidTableName");
111 "Cassandra SM: Db UseColumnFamily fqnTableName");
126 if (
mgr()->is_reinit_triggered())
return false;
150 boost::hash<string> string_hash;
157 set<string> uuid_list = req_list;
158 vector<GenDb::DbDataValueVec> keys;
159 for (set<string>::const_iterator it = uuid_list.begin();
160 it != uuid_list.end(); it++) {
162 key.push_back(
GenDb::Blob(
reinterpret_cast<const uint8_t *
>
163 (it->c_str()), it->size()));
167 GenDb::Blob col_filter(
reinterpret_cast<const uint8_t *
>(
"d"), 1);
173 field_vec.push_back(boost::make_tuple(
"key",
true,
false,
false));
174 field_vec.push_back(boost::make_tuple(
"column1",
false,
true,
false));
175 field_vec.push_back(boost::make_tuple(
"value",
false,
false,
true));
183 assert(col_list.
rowkey_.size() == 1);
187 string uuid_str(
reinterpret_cast<const char *
>(
uuid.data()),
190 uuid_list.erase(uuid_str);
198 "GetMultiRow failed for table",
213 BOOST_FOREACH(
string uuid_key, uuid_list) {
294 "Parsing row response for type/fq_name failed for table",
314 for (set<string>::iterator it =
317 pair<multimap<string, JsonAdapterDataType>::iterator,
318 multimap<string, JsonAdapterDataType>::iterator> ret =
320 for (multimap<string, JsonAdapterDataType>::iterator mit =
321 ret.first; mit != ret.second; mit++) {
322 cass_data_vec.push_back(mit->second);
335 assert(ncol.
name->size() == 1);
336 assert(ncol.
value->size() == 1);
341 GenDb::Blob dname_blob(boost::get<GenDb::Blob>(dname));
342 string key(
reinterpret_cast<const char *
>(dname_blob.
data()),
347 string value(boost::get<string>(dvalue));
351 uint64_t timestamp = boost::get<uint64_t>(dtimestamp);
353 cass_data_vec, context);
358 const string &
uuid,
const string &key,
const string &value,
365 cass_data_vec->push_back(adapter);
370 const string &uuid_key,
const string &obj_type,
385 "Cassandra SM: Post shutdown during re init");
394 ConfigClientMgrDebug,
"Cassandra SM: BulkDataSync Started");
419 for (ConfigClientManager::ObjectTypeList::const_iterator it =
420 mgr()->config_json_parser()->ObjectTypeListToRead().begin();
428 "Cassandra SM: Abort FQName reader on reinit trigger");
434 key.push_back(
GenDb::Blob(
reinterpret_cast<const uint8_t *
>
435 (it->c_str()), it->size()));
437 if (!column_name.empty()) {
438 GenDb::Blob col_filter(
reinterpret_cast<const uint8_t *
>
439 (column_name.c_str()), column_name.size());
443 boost::assign::list_of(
455 field_vec.push_back(boost::make_tuple(
"key",
true,
false,
false));
456 field_vec.push_back(boost::make_tuple(
"column1",
false,
true,
497 string *last_column) {
500 assert(ncol.
name->size() == 1);
503 GenDb::Blob dname_blob(boost::get<GenDb::Blob>(dname));
504 column_name = string(
reinterpret_cast<const char *
>(dname_blob.
data()),
509 *last_column = column_name;
518 uuid_list.push_back(make_pair(obj_type,
uuid_str));
523 const string &key)
const {
524 size_t temp = key.rfind(
':');
525 return (temp == string::npos) ?
"" : key.substr(temp+1);
530 for (ObjTypeUUIDList::const_iterator it = uuid_list.begin();
531 it != uuid_list.end(); it++) {
538 const string &search_string,
int inst_num,
const string &last_uuid,
539 uint32_t num_entries, vector<ConfigDBUUIDCacheEntry> *entries)
const {
541 num_entries, entries);
551 long num_config_readers_still_processing =
553 if (num_config_readers_still_processing == 1) {
555 "Cassandra SM: BulkSyncDone by all readers");
559 "Cassandra SM: One reader finished BulkSync");
570 process::ConnectionType::DATABASE,
"Cassandra",
571 process::ConnectionStatus::UP,
572 dbif_->Db_GetEndpoints(),
"Established Cassandra connection");
574 "Cassandra SM: Established Cassandra connection");
577 process::ConnectionType::DATABASE,
"Cassandra",
578 process::ConnectionStatus::DOWN,
579 dbif_->Db_GetEndpoints(),
"Lost Cassandra connection");
581 "Cassandra SM: Lost Cassandra connection");
586 const string &lookup_key) {
592 : config_client_(client), worker_id_(idx) {
619 const string &obj_type,
620 const string &uuid_str) {
621 pair<UUIDProcessSet::iterator, bool> ret;
632 ret.first->second->
oper = oper;
633 ret.first->second->uuid = uuid_str;
638 const string &
uuid,
bool add_change) {
642 if (obj_type_fq_name_pair.second ==
"ERROR") {
647 bool needNotify =
false;
648 std::string obj_type(
"");
656 for (FieldDetailMap::iterator it =
657 uuid_iter->second->GetFieldDetailMap().begin(), itnext;
658 it != uuid_iter->second->GetFieldDetailMap().end();
662 if (it->first.key ==
"type") {
663 obj_type = it->first.value;
664 obj_type.erase(remove(obj_type.begin(),
665 obj_type.end(),
'\"'), obj_type.end());
666 cass_data_vec.push_back(it->first);
669 if (it->first.key ==
"fq_name") {
670 cass_data_vec.push_back(it->first);
673 if (!add_change || !it->second.refreshed) {
674 if (it->first.key ==
"type" || it->first.key ==
"fq_name") {
679 cass_data_vec.push_back(it->first);
681 uuid_iter->second->GetFieldDetailMap().erase(it);
686 if (add_change !=
true) {
698 const string &lookup_key) {
705 key =
"propm:" + lookup_key;
706 FieldDetailMap::iterator lower_bound_it =
707 uuid_iter->second->GetFieldDetailMap().lower_bound(
709 if (lower_bound_it != uuid_iter->second->GetFieldDetailMap().end() &&
710 boost::starts_with(lower_bound_it->first.key, key)) {
713 key =
"propl:" + lookup_key;
715 uuid_iter->second->GetFieldDetailMap().lower_bound(
717 if (lower_bound_it != uuid_iter->second->GetFieldDetailMap().end() &&
718 boost::starts_with(lower_bound_it->first.key, key)) {
731 set<string> bunch_req_list;
732 int num_req_handled = 0;
734 for (UUIDProcessSet::iterator it =
uuid_read_set_.begin(), itnext;
741 if (obj_req->
oper ==
"CREATE" || obj_req->
oper ==
"UPDATE" ||
742 obj_req->
oper ==
"UPDATE-IMPLICIT") {
743 bunch_req_list.insert(obj_req->
uuid);
750 num_req_handled += bunch_req_list.size();
752 if (num_req_handled >=
client()->GetMaxRequestsToYield()) {
757 }
else if (obj_req->
oper ==
"DELETE") {
759 }
else if (obj_req->
oper ==
"EndOfConfig") {
763 if (++num_req_handled ==
client()->GetMaxRequestsToYield()) {
769 if (!bunch_req_list.empty() && !
client()->mgr()->is_reinit_triggered()) {
775 if (
client()->mgr()->is_reinit_triggered()) {
777 "Cassandra SM: Clear UUID read set due to reinit");
785 BOOST_FOREACH(
string uuid, req_list) {
792 UUIDProcessSet::iterator req_it =
794 delete req_it->second;
808 return uuid_iter->second;
816 return uuid_iter->second;
821 uint32_t retry_time_pow_of_two =
837 *parent_->client()->event_manager()->io_service(),
838 "UUID retry timer for " +
uuid,
840 "config_client::Reader"),
841 parent_->worker_id_);
843 "Created UUID read retry timer ",
uuid);
845 retry_timer_->Cancel();
846 retry_timer_->Start(parent_->UUIDRetryTimeInMSec(
this),
854 "Start/restart UUID Read Retry timer due to configuration",
uuid);
860 retry_timer_->Cancel();
865 "UUID Read retry timer - deleted timer due to configuration",
872 return (retry_timer_->running());
878 parent_->client()->mgr()->EnqueueUUIDRequest(
879 "UPDATE", GetObjType(), parent_->client()->uuid_str(
uuid));
887 std::string message =
"Timer";
889 "UUID Read Retry Timer error ", message, message);
894 for (set<string>::iterator it =
903 FieldDetailMap::iterator field_iter =
904 uuid_iter->second->GetFieldDetailMap().lower_bound(
906 assert(field_iter != uuid_iter->second->GetFieldDetailMap().end());
907 assert(it->compare(0, it->size() - 1, field_iter->first.key,
908 0, it->size() - 1) == 0);
909 while (it->compare(0, it->size() - 1, field_iter->first.key,
910 0, it->size() - 1) == 0) {
911 if (field_iter->second.refreshed ==
false) {
925 size_t from_front_pos = adapter->
key.find(
':');
926 size_t from_back_pos = adapter->
key.rfind(
':');
927 string type_field = adapter->
key.substr(0, from_front_pos+1);
934 string prop_name = adapter->
key.substr(from_front_pos+1);
942 if ((prop_name.compare(
"draft_mode_state") == 0) &&
943 !adapter->
value.empty()) {
950 string prop_name =
"";
951 if (is_ref || is_parent) {
952 string ref_uuid = adapter->
key.substr(from_back_pos+1);
955 if (ref_name ==
"ERROR") {
958 "Out of order parent or ref",
uuid +
":" + adapter->
key);
964 }
else if (is_propl || is_propm) {
965 prop_name = adapter->
key.substr(0, from_back_pos);
970 if (adapter->
key.compare(
"type") == 0) {
976 }
else if (adapter->
key.compare(
"fq_name") == 0) {
984 replace(context.
fq_name.begin(), context.
fq_name.end(),
',',
':');
987 FieldDetailMap::iterator field_iter =
988 uuid_iter->second->GetFieldDetailMap().find(*adapter);
989 if (field_iter == uuid_iter->second->GetFieldDetailMap().end()) {
994 uuid_iter->second->GetFieldDetailMap().insert(make_pair
995 (*adapter, field_ts_info));
997 field_iter->second.refreshed =
true;
998 if (
client()->SkipTimeStampCheckForTypeAndFQName() &&
999 ((adapter->
key.compare(
"type") == 0) ||
1000 (adapter->
key.compare(
"fq_name") == 0))) {
1003 if (timestamp && field_iter->second.time_stamp == timestamp) {
1004 if (is_propl || is_propm) {
1009 field_iter->second.time_stamp = timestamp;
1011 if (is_propl || is_propm) {
1024 string tmp_uuid =
uuid;
1026 pair<ObjectCacheMap::iterator, bool> ret_uuid =
1028 assert(ret_uuid.second);
1029 uuid_iter = ret_uuid.first;
1033 for (FieldDetailMap::iterator it =
1034 uuid_iter->second->GetFieldDetailMap().begin();
1035 it != uuid_iter->second->GetFieldDetailMap().end(); it++) {
1036 it->second.refreshed =
false;
1038 return uuid_iter->second;
1042 ObjectCacheMap::const_iterator uuid_iter,
1043 ConfigDBUUIDCacheEntry *entry)
const {
1044 entry->set_uuid(
uuid);
1045 entry->set_timestamp(
1047 entry->set_retry_count(uuid_iter->second->GetRetryCount());
1048 entry->set_fq_name(uuid_iter->second->GetFQName());
1049 entry->set_obj_type(uuid_iter->second->GetObjType());
1050 entry->set_timer_running(uuid_iter->second->IsRetryTimerRunning());
1051 entry->set_timer_created(uuid_iter->second->IsRetryTimerCreated());
1052 vector<ConfigDBUUIDCacheData> fields;
1053 for (FieldDetailMap::const_iterator it =
1054 uuid_iter->second->GetFieldDetailMap().begin();
1055 it != uuid_iter->second->GetFieldDetailMap().end(); it++) {
1056 ConfigDBUUIDCacheData each_field;
1057 each_field.set_refresh(it->second.refreshed);
1058 each_field.set_field_name(it->first.key);
1060 fields.push_back(each_field);
1062 entry->set_field_list(fields);
1066 const string &search_string,
const string &last_uuid, uint32_t num_entries,
1067 vector<ConfigDBUUIDCacheEntry> *entries)
const {
1069 regex search_expr(search_string);
1070 for (ObjectCacheMap::const_iterator it =
1077 ConfigDBUUIDCacheEntry entry;
1079 entries->push_back(entry);
static const std::string parent_prefix
static const std::string prop_prefix
static const std::string map_prop_prefix
static const std::string ref_prefix
static const std::string list_prop_prefix
ConfigCassandraPartition * GetPartition(const std::string &uuid)
virtual bool IsListOrMapPropEmpty(const string &uuid_key, const string &lookup_key)
virtual void PostShutdown()
std::atomic< long > bulk_sync_status_
virtual void InitDatabase()
virtual ~ConfigCassandraClient()
bool EnqueueDBSyncRequest(const ObjTypeUUIDList &uuid_list)
static const std::string kUuidTableName
virtual bool UUIDToObjCacheShow(const std::string &search_string, int inst_num, const std::string &last_uuid, uint32_t num_entries, std::vector< ConfigDBUUIDCacheEntry > *entries) const
virtual int HashUUID(const std::string &uuid_str) const
std::list< ObjTypeUUIDType > ObjTypeUUIDList
boost::scoped_ptr< TaskTrigger > fq_name_reader_
virtual std::string FetchUUIDFromFQNameEntry(const std::string &key) const
static const std::string kFqnTableName
PartitionList partitions_
static const std::string kObjectProcessTaskId
void EnqueueUUIDRequest(std::string oper, std::string obj_type, std::string uuid_str)
void UpdateFQNameCache(const std::string &key, const std::string &obj_type, ObjTypeUUIDList &uuid_list)
void HandleCassandraConnectionStatus(bool success, bool force_update=false)
virtual bool IsTaskTriggered() const
bool ParseFQNameRowGetUUIDList(const std::string &obj_type, const GenDb::ColList &col_list, ObjTypeUUIDList &uuid_list, std::string *last_column)
virtual bool BulkDataSync()
static const std::string kCassClientTaskId
virtual uint32_t GetFQNameEntriesToRead() const
ConfigCassandraClient(ConfigClientManager *mgr, EventManager *evm, const ConfigClientOptions &options, int num_workers)
void EnableCassandraReadRetry(const std::string uuid)
uint32_t GetRetryCount() const
bool CassReadRetryTimerExpired(const std::string uuid)
void CassReadRetryTimerErrorHandler()
bool IsRetryTimerRunning() const
void DisableCassandraReadRetry(const std::string uuid)
bool IsTaskTriggered() const
void RemoveObjReqEntry(std::string &uuid)
UUIDProcessSet uuid_read_set_
static const uint32_t kMinUUIDRetryTimeMSec
void ParseObjUUIDTableEachColumnBuildContext(const std::string &uuid, const std::string &key, const std::string &value, uint64_t timestamp, CassColumnKVVec *cass_data_vec, ConfigCassandraParseContext &context)
ObjProcessWorkQType obj_process_queue_
void DeleteCacheMap(const std::string &uuid)
virtual bool ReadObjUUIDTable(const std::set< std::string > &uuid_list)
ObjectCacheMap object_cache_map_
void AddUUIDToRequestList(const std::string &oper, const std::string &obj_type, const std::string &uuid_str)
bool RequestHandler(ObjectProcessReq *req)
ConfigCassandraClient * client()
bool IsListOrMapPropEmpty(const string &uuid_key, const string &lookup_key)
boost::asio::io_context * ioservice()
virtual void GenerateAndPushJson(const string &uuid_key, const string &obj_type, const CassColumnKVVec &cass_data_vec, bool add_change)
bool UUIDToObjCacheShow(const std::string &search_string, const std::string &last_uuid, uint32_t num_entries, std::vector< ConfigDBUUIDCacheEntry > *entries) const
virtual int UUIDRetryTimeInMSec(const ObjCacheEntry *obj) const
virtual void HandleObjectDelete(const string &uuid, bool add_change)
bool ProcessObjUUIDTableEntry(const std::string &uuid_key, const GenDb::ColList &col_list)
boost::shared_ptr< TaskTrigger > config_reader_
ObjCacheEntry * GetObjCacheEntry(const std::string &uuid)
ConfigCassandraPartition(ConfigCassandraClient *client, size_t idx)
void Enqueue(ObjectProcessReq *req)
ObjCacheEntry * MarkCacheDirty(const std::string &uuid)
static const uint32_t kMaxUUIDRetryTimePowOfTwo
virtual ~ConfigCassandraPartition()
void ListMapPropReviseUpdateList(const std::string &uuid, ConfigCassandraParseContext &context)
void RemoveObjReqEntries(std::set< std::string > &req_list)
bool StoreKeyIfUpdated(const std::string &uuid, JsonAdapterDataType *adapter, uint64_t timestamp, ConfigCassandraParseContext &context)
virtual void ParseObjUUIDTableEntry(const std::string &uuid, const GenDb::ColList &col_list, CassColumnKVVec *cass_data_vec, ConfigCassandraParseContext &context)
void FillUUIDToObjCacheInfo(const std::string &uuid, ObjectCacheMap::const_iterator uuid_iter, ConfigDBUUIDCacheEntry *entry) const
static const std::set< std::string > skip_properties
bool is_reinit_triggered()
ConfigJsonParserBase * config_json_parser()
virtual std::string uuid_str(const std::string &uuid)
EventManager * event_manager()
std::pair< std::string, std::string > ObjTypeFQNPair
const std::vector< std::string > & config_db_ips() const
ObjTypeFQNPair UUIDToFQName(const std::string &uuid_str, bool deleted_ok=true) const
virtual uint32_t GetNumReadRequestToBunch() const
int GetFirstConfigDbPort() const
virtual void UpdateConnectionInfo(bool success, bool force)
virtual void AddFQNameCache(const std::string &uuid, const std::string &obj_type, const std::string &fq_name)
const std::string & config_db_password() const
virtual void InitConnectionInfo()
virtual void PurgeFQNameCache(const std::string &uuid)
virtual void ClearFQNameCache()
ConfigClientManager * mgr()
const std::string & config_db_user() const
virtual const uint64_t GetInitRetryTimeUSec() const
const ObjectTypeList & ObjectTypeListToRead() const
virtual bool Receive(const ConfigCass2JsonAdapter &adapter, bool add_change)=0
boost::asio::io_context * io_service()
virtual void SetFQName(std::string fq_name)
virtual void SetObjType(std::string obj_type)
int GetTaskId(const std::string &name)
static TaskScheduler * GetInstance()
static bool DeleteTimer(Timer *Timer)
static Timer * CreateTimer(boost::asio::io_context &service, const std::string &name, int task_id=Timer::GetTimerTaskId(), int task_instance=Timer::GetTimerInstanceId(), bool delete_on_completion=false)
static ConnectionState * GetInstance()
#define CONFIG_CLIENT_DEBUG(obj,...)
#define CONFIG_CLIENT_WARN(obj,...)
std::vector< JsonAdapterDataType > CassColumnKVVec
std::vector< DbDataValue > DbDataValueVec
boost::variant< boost::blank, std::string, uint64_t, uint32_t, boost::uuids::uuid, uint8_t, uint16_t, double, IpAddress, Blob > DbDataValue
std::vector< FieldNamesToReadInfo > FieldNamesToReadVec
boost::ptr_vector< ColList > ColListVec
static bool regex_search(const std::string &input, const regex ®ex)
static bool regex_match(const std::string &input, const regex ®ex)
set< string > candidate_list_map_properties
set< string > updated_list_map_properties
bool parent_or_ref_fq_name_unknown
ConfigCassandraParseContext()
std::multimap< string, JsonAdapterDataType > list_map_properties
DISALLOW_COPY_AND_ASSIGN(ConfigCassandraParseContext)
const uint8_t * data() const
boost::scoped_ptr< DbDataValueVec > value
boost::scoped_ptr< DbDataValueVec > timestamp
boost::scoped_ptr< DbDataValueVec > name
#define CHECK_CONCURRENCY(...)
static std::string UTCUsecToString(uint64_t tstamp)
static uint64_t UTCTimestampUsec()
void STLDeleteValues(Container *container)