1 /*
2  Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights
3  reserved.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License, version 2.0,
7  as published by the Free Software Foundation.
8 
9  This program is also distributed with certain software (including
10  but not limited to OpenSSL) that is licensed under separate terms,
11  as designated in a particular file or component or in included license
12  documentation.  The authors of MySQL hereby grant you an additional
13  permission to link the program and your derivative works with the
14  separately licensed software that they have included with MySQL.
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  GNU General Public License, version 2.0, for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24  02110-1301  USA
25  */
26 
27 #include <memcached/types.h>
28 #include <memcached/extension_loggers.h>
29 
30 #include <NdbApi.hpp>
31 
32 #include "atomics.h"
33 #include "Configuration.h"
34 #include "QueryPlan.h"
35 #include "SchedulerConfigManager.h"
36 
SchedulerConfigManager(int _thread,int _cluster)37 SchedulerConfigManager::SchedulerConfigManager(int _thread, int _cluster) :
38   thread(_thread),
39   cluster(_cluster),
40   current_plans(0),
41   old_plans(0),
42   nstatreq(0)
43 {
44   DEBUG_ENTER_DETAIL();
45 }
46 
47 
~SchedulerConfigManager()48 SchedulerConfigManager::~SchedulerConfigManager() {
49   DEBUG_ENTER_DETAIL();
50   if(current_plans) delete current_plans;
51   if(old_plans) delete old_plans;
52 }
53 
54 
55 /* This is a partial implementation of online reconfiguration.
56    It can replace KeyPrefix mappings, but not add a cluster at runtime
57 */
configure(Configuration * conf)58 void SchedulerConfigManager::configure(Configuration *conf) {
59   DEBUG_ENTER();
60 
61   /* Get my Ndb Cluster Connection */
62   ndb_connection = conf->getConnectionPoolById(cluster)->getPooledConnection(thread);
63 
64   /* Get a set of QueryPlans for the new configuration */
65   ConnQueryPlanSet * plans = new ConnQueryPlanSet(ndb_connection, conf->nprefixes);
66   plans->buildSetForConfiguration(conf, cluster);
67 
68   /* Garbage Collect old old plans */
69   if(old_plans) {
70     delete old_plans;
71   }
72   /* Save current plans as old ones */
73   old_plans = (ConnQueryPlanSet*) current_plans;
74 
75   /* Swap new plans into place */
76   atomic_set_ptr((void * volatile *)& current_plans, plans);
77 }
78 
79 
setQueryPlanInWorkitem(workitem * item)80 const KeyPrefix * SchedulerConfigManager::setQueryPlanInWorkitem(workitem * item) {
81   const ConnQueryPlanSet * plans = current_plans;
82   const KeyPrefix * prefix =
83     plans->getConfiguration()->getPrefixByInfo(item->prefix_info);
84 
85   /* Set length in workitem */
86   item->base.nsuffix = item->base.nkey - prefix->prefix_len;
87 
88   /* Set QueryPlan in workitem */
89   item->plan = plans->getPlanForPrefix(prefix);
90 
91   return prefix;
92 }
93 
94 
add_stats(const char * stat_key,ADD_STAT add_stat,const void * cookie)95 void SchedulerConfigManager::add_stats(const char *stat_key,
96                                        ADD_STAT add_stat,
97                                        const void *cookie) {
98   if(strncasecmp(stat_key, "reconf", 6) == 0) {
99     const char * key = "Running";
100     char buffer[16];
101     int gen = current_plans->getConfiguration()->generation;
102     int value_len = snprintf(buffer, 16, "%d", gen);
103     add_stat(key, strlen(key), buffer, value_len, cookie);
104     DEBUG_PRINT("stats reconf [req %d]: running %d", ++nstatreq, gen);
105   }
106 }
107 
108