hellopics-mgmt_ha.c

Go to the documentation of this file.
00001 /*
00002  * This code is provided as is by Juniper Networks SDK Developer Support.
00003  * It is provided with no warranties or guarantees, and Juniper Networks
00004  * will not provide support or maintenance of this code in any fashion.
00005  * The code is provided only to help a developer better understand how
00006  * the SDK can be used.
00007  * 
00008  * Copyright (c) 2008, Juniper Networks, Inc.
00009  * All rights reserved.
00010  */
00011 
00020 #include <stdlib.h>
00021 #include <string.h>
00022 #include <jnx/aux_types.h>
00023 #include <jnx/trace.h>
00024 #include <jnx/junos_trace.h>
00025 #include <netinet/in.h>
00026 #include <arpa/inet.h>
00027 #include "hellopics-mgmt_logging.h"
00028 #include "hellopics-mgmt_conn.h"
00029 #include "hellopics-mgmt_ha.h"
00030 #include <jnx/junos_sync.h>
00031 
00032 #include HELLOPICS_OUT_H
00033 /*** Constants ***/
00034 
00035 #define HELLOPICS_HA_REPLICATION_PORT 39089  
00036 
00037 #define HELLOPICS_HA_REPLICATION_KA 30       
00038 
00039 /*** Data structures ***/
00040 
00041 static junos_sync_state_t     rep_state;     
00042 static junos_sync_callbacks_t rep_callbacks; 
00043 static junos_sync_context_t * rep_ctx;       
00044 volatile boolean is_master;
00045 
00046 extern hellopics_stats_t  hellopics_stats;
00047 extern evContext present_ctx;
00048 
00049 /*** STATIC/INTERNAL Functions ***/
00050 void update_hellopics_statistics(hellopics_stats_t *data);
00051 
00052 
00059 static void
00060 replication_data_hton(hellopics_stats_t * data)
00061 {
00062     data->msgs_sent = htonl(data->msgs_sent);
00063     data->msgs_received = htonl(data->msgs_received);
00064     data->msgs_missed = htonl(data->msgs_missed);
00065     data->msgs_badorder = htonl(data->msgs_badorder);
00066 }
00067 
00068 
00075 static void
00076 replication_data_ntoh(hellopics_stats_t * data)
00077 {
00078     data->msgs_sent = ntohl(data->msgs_sent);
00079     data->msgs_received = ntohl(data->msgs_received);
00080     data->msgs_missed = ntohl(data->msgs_missed);
00081     data->msgs_badorder = ntohl(data->msgs_badorder);
00082 }
00083 
00084 
00085 
00095 static void
00096 ack_notify(junos_sync_tlv_t * data UNUSED, size_t len UNUSED)
00097 {
00098     LOG(LOG_INFO, "%s: len %d", __func__, len);
00099 }
00100 
00101 
00112 static void
00113 decode(void * data, size_t len)
00114 {
00115    INSIST_ERR(!rep_state.is_master);
00116    LOG(LOG_INFO, "%s: update message of length %d", __func__, len);
00117    replication_data_ntoh((hellopics_stats_t *)data);
00118    update_hellopics_statistics((hellopics_stats_t *)data);
00119 }
00120 
00121 
00134 static void *
00135 get_next(void * current __unused , junos_sync_getnext_data_t * gndata __unused)
00136 {
00137   return NULL;
00138 }
00139 
00140 
00147 static void
00148 conn_status_change (junos_sync_conn_status_t conn_status)
00149 {
00150     LOG(LOG_INFO, "%s: replication connection status is %s", __func__,
00151         (conn_status == JUNOS_SYNC_CONN_DOWN) ? "Down" : "Up");
00152 }
00153 
00154 
00161 static void
00162 init_sync_done (size_t num_of_entries_synced)
00163 {
00164     LOG(LOG_INFO, "%s: initial replication is done, %d entries are synced",
00165             __func__, num_of_entries_synced);
00166 }
00167 
00168 
00175 static void
00176 bind_done(in_port_t port)
00177 {
00178     if(port != HELLOPICS_HA_REPLICATION_PORT) {
00179         LOG(LOG_EMERG, "%s: master's replication server could not bind "
00180                 "to port %d", __func__, HELLOPICS_HA_REPLICATION_PORT);
00181         return;
00182     }
00183     
00184     LOG(LOG_INFO, "%s: master's replication server is bound to port %d",
00185             __func__, port);
00186 }
00187 
00188 
00195 static void
00196 log_msg(const char * format, ...)
00197 {
00198     va_list ap;
00199     va_start(ap, format);
00200     vlogging(LOG_INFO, format, ap);
00201     va_end(ap);
00202 }
00203 
00204 
00208 static void
00209 db_clear(void)
00210 {
00211     LOG(LOG_INFO, "%s", __func__);
00212 }
00213 
00214 
00215 /*** GLOBAL/EXTERNAL Functions ***/
00216 
00217 
00227 status_t
00228 init_replication(in_addr_t master_address)
00229 {
00230     int result;
00231     rep_ctx = NULL;
00232     bzero(&rep_state, sizeof(junos_sync_state_t));
00233     bzero(&rep_callbacks, sizeof(junos_sync_callbacks_t));
00234     
00235     rep_state.is_master = (master_address == 0);
00236     rep_state.no_resync = false;
00237     rep_state.server_port = HELLOPICS_HA_REPLICATION_PORT;
00238     rep_state.keepalive_time = HELLOPICS_HA_REPLICATION_KA;
00239     
00240     if(!rep_state.is_master) {
00241         rep_state.server_addr = master_address;
00242     }
00243     
00244     rep_callbacks.sync_ack_notify_func = ack_notify;
00245     rep_callbacks.sync_data_decode_func = decode;
00246     rep_callbacks.sync_data_get_next_func = get_next;
00247     rep_callbacks.sync_conn_status_change_func = conn_status_change;
00248     rep_callbacks.sync_init_sync_done_func = init_sync_done;
00249     rep_callbacks.sync_bind_done_func = bind_done;
00250     rep_callbacks.sync_log_msg_func = log_msg;
00251     rep_callbacks.sync_malloc_func = malloc;
00252     rep_callbacks.sync_free_func = free;
00253     rep_callbacks.sync_db_clear_func = db_clear;
00254 
00255     rep_ctx = junos_sync_init(&rep_state, &rep_callbacks);
00256     
00257     if (!rep_ctx) {
00258         LOG(LOG_ERR, "%s: Failed to initialize a junos_sync context.",
00259             __func__);
00260         return EFAIL;
00261     }
00262     
00263     if ((result = junos_sync_set_event_context(rep_ctx, present_ctx)) != JUNOS_SYNC_OK) {
00264         LOG(LOG_ERR, "%s: Failed to set an event context. Error is %d",
00265                     __func__,result);
00266         junos_sync_exit(rep_ctx);
00267     }
00268     if ((result = junos_sync_start(rep_ctx)) != JUNOS_SYNC_OK) {
00269         LOG(LOG_ERR, "%s: Failed to start a junos_sync subsystem. Error is %d",
00270                     __func__,result);
00271         junos_sync_exit(rep_ctx);
00272         return EFAIL;
00273     }
00274       LOG(LOG_INFO, 
00275             "%s: Intialized and started sync", __func__);
00276     return SUCCESS;
00277 }
00278 
00279 
00286 void
00287 update_replication_entry(hellopics_stats_t *data)
00288 {
00289     hellopics_stats_t * current;
00290     junos_sync_tlv_t * tlv_data;
00291     
00292     if(rep_ctx == NULL)
00293         return;
00294     
00295     LOG(LOG_INFO, "%s", __func__);
00296     
00297     // create a new tlv with the trailing data
00298     tlv_data = calloc(1, sizeof(junos_sync_tlv_t) + sizeof(hellopics_stats_t));
00299     INSIST_ERR(tlv_data != NULL);
00300     
00301     tlv_data->tlv_len = sizeof(hellopics_stats_t);
00302     current = (hellopics_stats_t *)&tlv_data->tlv_value;
00303 
00304     // populate data to enqueue
00305     memcpy(current, data, sizeof(hellopics_stats_t));
00306     
00307     replication_data_hton(current);
00308     
00309       LOG(LOG_INFO, 
00310             "%s: Master:Queueing the data for replication", __func__);
00311     junos_sync_queue(rep_ctx, tlv_data, FALSE);
00312 }
00313 
00314 
00318 void
00319 stop_replication(void)
00320 {
00321     junos_sync_exit(rep_ctx);
00322     rep_ctx = NULL;
00323 }
00324 
00331 void update_hellopics_statistics(hellopics_stats_t *data)
00332 {
00333     LOG(LOG_INFO, "%s: Backup: Received replication data",
00334             __func__);
00335     hellopics_stats.msgs_sent      = data->msgs_sent;
00336     hellopics_stats.msgs_received  = data->msgs_received;
00337     hellopics_stats.msgs_missed    = data->msgs_missed;
00338     hellopics_stats.msgs_badorder  = data->msgs_badorder;
00339     LOG(LOG_INFO, "Backup: Messages sent-%d, received-%d, missed-%d, badorder-%d",
00340                     hellopics_stats.msgs_sent,hellopics_stats.msgs_received, 
00341                     hellopics_stats.msgs_missed,hellopics_stats.msgs_badorder);
00342 }
00352 void
00353 set_mastership(boolean state, in_addr_t master_addr)
00354 {
00355      if(init_replication(master_addr) == SUCCESS) {
00356 
00357         is_master = state;
00358     }
00359 }

2007-2009 Juniper Networks, Inc. All rights reserved. The information contained herein is confidential information of Juniper Networks, Inc., and may not be used, disclosed, distributed, modified, or copied without the prior written consent of Juniper Networks, Inc. in an express license. This information is subject to change by Juniper Networks, Inc. Juniper Networks, the Juniper Networks logo, and JUNOS are registered trademarks of Juniper Networks, Inc. in the United States and other countries. All other trademarks, service marks, registered trademarks, or registered service marks are the property of their respective owners.
Generated on Sun May 30 20:26:58 2010 for SDK Your Net Corporation Hellopics Example: hellopics-mgmt 1.0 by Doxygen 1.5.1