• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

RecLocal.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003   Record local definitions
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 #include "libts.h"
00025 #include "Rollback.h"
00026 #include "ParseRules.h"
00027 #include "P_RecCore.h"
00028 #include "P_RecLocal.h"
00029 #include "P_RecMessage.h"
00030 #include "P_RecUtils.h"
00031 #include "P_RecFile.h"
00032 #include "LocalManager.h"
00033 #include "FileManager.h"
00034 
00035 // Marks whether the message handler has been initialized.
00036 static bool message_initialized_p = false;
00037 
00038 //-------------------------------------------------------------------------
00039 // i_am_the_record_owner, only used for libreclocal.a
00040 //-------------------------------------------------------------------------
00041 bool
00042 i_am_the_record_owner(RecT rec_type)
00043 {
00044   switch (rec_type) {
00045   case RECT_CONFIG:
00046   case RECT_NODE:
00047   case RECT_CLUSTER:
00048   case RECT_LOCAL:
00049     return true;
00050   case RECT_PROCESS:
00051   case RECT_PLUGIN:
00052     return false;
00053   default:
00054     ink_assert(!"Unexpected RecT type");
00055     return false;
00056   }
00057 }
00058 
00059 //-------------------------------------------------------------------------
00060 // sync_thr
00061 //-------------------------------------------------------------------------
00062 static void *
00063 sync_thr(void * data)
00064 {
00065   textBuffer *tb = new textBuffer(65536);
00066   FileManager * configFiles = (FileManager *)data;
00067 
00068   Rollback *rb;
00069   bool inc_version;
00070   bool written;
00071 
00072   while (1) {
00073     send_push_message();
00074     RecSyncStatsFile();
00075     if (RecSyncConfigToTB(tb, &inc_version) == REC_ERR_OKAY) {
00076       written = false;
00077       if (configFiles->getRollbackObj(REC_CONFIG_FILE, &rb)) {
00078         if (inc_version) {
00079           RecDebug(DL_Note, "Rollback: '%s'", REC_CONFIG_FILE);
00080           version_t ver = rb->getCurrentVersion();
00081           if ((rb->updateVersion(tb, ver, -1, false)) != OK_ROLLBACK) {
00082             RecDebug(DL_Note, "Rollback failed: '%s'", REC_CONFIG_FILE);
00083           }
00084           written = true;
00085         }
00086       }
00087       else {
00088         rb = NULL;
00089       }
00090       if (!written) {
00091         RecWriteConfigFile(tb);
00092         if (rb != NULL) {
00093           rb->setLastModifiedTime();
00094         }
00095       }
00096     }
00097     usleep(REC_REMOTE_SYNC_INTERVAL_MS * 1000);
00098   }
00099   return NULL;
00100 }
00101 
00102 
00103 //-------------------------------------------------------------------------
00104 // config_update_thr
00105 //-------------------------------------------------------------------------
00106 static void *
00107 config_update_thr(void * /* data */)
00108 {
00109   while (true) {
00110     RecExecConfigUpdateCbs(REC_LOCAL_UPDATE_REQUIRED);
00111     usleep(REC_CONFIG_UPDATE_INTERVAL_MS * 1000);
00112   }
00113   return NULL;
00114 }
00115 
00116 
00117 //-------------------------------------------------------------------------
00118 // RecMessageInit
00119 //-------------------------------------------------------------------------
00120 void
00121 RecMessageInit()
00122 {
00123   ink_assert(g_mode_type != RECM_NULL);
00124   lmgmt->registerMgmtCallback(MGMT_SIGNAL_LIBRECORDS, RecMessageRecvThis, NULL);
00125   message_initialized_p = true;
00126 }
00127 
00128 //-------------------------------------------------------------------------
00129 // RecLocalInit
00130 //-------------------------------------------------------------------------
00131 int
00132 RecLocalInit(Diags * _diags)
00133 {
00134   static bool initialized_p = false;;
00135 
00136   if (initialized_p) {
00137     return REC_ERR_OKAY;
00138   }
00139 
00140   g_mode_type = RECM_SERVER;
00141 
00142   if (RecCoreInit(RECM_SERVER, _diags) == REC_ERR_FAIL) {
00143     return REC_ERR_FAIL;
00144   }
00145 
00146   /* -- defer RecMessageInit() until LocalManager is initialized
00147      if (RecMessageInit(RECM_SERVER) == REC_ERR_FAIL) {
00148      return REC_ERR_FAIL;
00149      }
00150 
00151      if (RecMessageRegisterRecvCb(recv_message_cb, NULL)) {
00152      return REC_ERR_FAIL;
00153      }
00154    */
00155   initialized_p = true;
00156 
00157   return REC_ERR_OKAY;
00158 }
00159 
00160 
00161 //-------------------------------------------------------------------------
00162 // RecLocalInitMessage
00163 //-------------------------------------------------------------------------
00164 int
00165 RecLocalInitMessage()
00166 {
00167   static bool initialized_p = false;
00168 
00169   if (initialized_p) {
00170     return REC_ERR_OKAY;
00171   }
00172 
00173   RecMessageInit();
00174   if (RecMessageRegisterRecvCb(recv_message_cb, NULL)) {
00175     return REC_ERR_FAIL;
00176   }
00177 
00178   initialized_p = true;
00179 
00180   return REC_ERR_OKAY;
00181 }
00182 
00183 //-------------------------------------------------------------------------
00184 // RecLocalStart
00185 //-------------------------------------------------------------------------
00186 int
00187 RecLocalStart(FileManager * configFiles)
00188 {
00189   ink_thread_create(sync_thr, configFiles);
00190   ink_thread_create(config_update_thr, NULL);
00191 
00192   return REC_ERR_OKAY;
00193 }
00194 
00195 int
00196 RecRegisterManagerCb(int id, RecManagerCb _fn, void *_data)
00197 {
00198   return lmgmt->registerMgmtCallback(id, _fn, _data);
00199 }
00200 
00201 void
00202 RecSignalManager(int id, const char *, size_t)
00203 {
00204    // Signals are messages sent across the management pipe, so by definition,
00205    // you can't send a signal if you are a local process manager.
00206    RecDebug(DL_Debug, "local manager dropping signal %d", id);
00207 }
00208 
00209 //-------------------------------------------------------------------------
00210 // RecMessageSend
00211 //-------------------------------------------------------------------------
00212 
00213 int
00214 RecMessageSend(RecMessage * msg)
00215 {
00216   int msg_size;
00217 
00218   if (!message_initialized_p)
00219     return REC_ERR_OKAY;
00220 
00221   // Make a copy of the record, but truncate it to the size actually used
00222   if (g_mode_type == RECM_CLIENT || g_mode_type == RECM_SERVER) {
00223     msg->o_end = msg->o_write;
00224     msg_size = sizeof(RecMessageHdr) + (msg->o_write - msg->o_start);
00225     lmgmt->signalEvent(MGMT_EVENT_LIBRECORDS, (char *) msg, msg_size);
00226   }
00227 
00228   return REC_ERR_OKAY;
00229 }
00230 

Generated by  doxygen 1.7.1