Exemplo n.º 1
0
/**
 * Initialise the polling system we are using for the gateway.
 *
 * In this case we are using the Linux epoll mechanism
 */
void
poll_init()
{
int	i;

	if (epoll_fd != -1)
		return;
	if ((epoll_fd = epoll_create(MAX_EVENTS)) == -1)
	{
		perror("epoll_create");
		exit(-1);
	}
	memset(&pollStats, 0, sizeof(pollStats));
	memset(&queueStats, 0, sizeof(queueStats));
	bitmask_init(&poll_mask);
        n_threads = config_threadcount();
	if ((thread_data =
		(THREAD_DATA *)malloc(n_threads * sizeof(THREAD_DATA))) != NULL)
	{
		for (i = 0; i < n_threads; i++)
		{
			thread_data[i].state = THREAD_STOPPED;
		}
	}
#if MUTEX_EPOLL
        simple_mutex_init(&epoll_wait_mutex, "epoll_wait_mutex");        
#endif

	hktask_add("Load Average", poll_loadav, NULL, POLL_LOAD_FREQ);
	n_avg_samples = 15 * 60 / POLL_LOAD_FREQ;
	avg_samples = (double *)malloc(sizeof(double) * n_avg_samples);
	for (i = 0; i < n_avg_samples; i++)
		avg_samples[i] = 0.0;
	evqp_samples = (int *)malloc(sizeof(int) * n_avg_samples);
	for (i = 0; i < n_avg_samples; i++)
		evqp_samples[i] = 0.0;

	number_poll_spins = config_nbpolls();
	max_poll_sleep = config_pollsleep();

#if PROFILE_POLL
	plog = memlog_create("EventQueueWaitTime", ML_LONG, 10000);
#endif
}
Exemplo n.º 2
0
/**
 * Start a service
 *
 * This function loads the protocol modules for each port on which the
 * service listens and starts the listener on that port
 *
 * Also create the router_instance for the service.
 *
 * @param service	The Service that should be started
 * @return	Returns the number of listeners created
 */
int
serviceStart(SERVICE *service)
{
SERV_PROTOCOL	*port;
int		listeners = 0;

	if ((service->router_instance = service->router->createInstance(service,
					service->routerOptions)) == NULL)
	{
		LOGIF(LE, (skygw_log_write_flush(LOGFILE_ERROR,
			"%s: Failed to create router instance for service. Service not started.",
				service->name)));
		service->state = SERVICE_STATE_FAILED;
		return 0;
	}

	port = service->ports;
	while (!service->svc_do_shutdown && port)
	{
		listeners += serviceStartPort(service, port);
		port = port->next;
	}
	if (listeners)
	{
		service->state = SERVICE_STATE_STARTED;
		service->stats.started = time(0);
	}

	/** Add the task that monitors session timeouts */
	if(service->conn_timeout > 0)
	{
	    hktask_add("connection_timeout",session_close_timeouts,NULL,5);
	}

	return listeners;
}
Exemplo n.º 3
0
/**
 * Create an instance of the filter for a particular service
 * within MaxScale.
 * 
 * @param options	The options for this filter
 *
 * @return The instance data for this new instance
 */
static	FILTER	*
createInstance(char **options, FILTER_PARAMETER **params)
{
  MQ_INSTANCE	*my_instance;
  int paramcount = 0, parammax = 64, i = 0, x = 0, arrsize = 0;
  FILTER_PARAMETER** paramlist;  
  char** arr;
  char taskname[512];
  
  if ((my_instance = calloc(1, sizeof(MQ_INSTANCE))))
    {
      spinlock_init(&my_instance->rconn_lock);
      spinlock_init(&my_instance->msg_lock);
      uid_gen = 0;
      paramlist = malloc(sizeof(FILTER_PARAMETER*)*64);

      if((my_instance->conn = amqp_new_connection()) == NULL){


	return NULL;
      }
      my_instance->channel = 1;
      my_instance->last_rconn = time(NULL);
      my_instance->conn_stat = AMQP_STATUS_OK;
      my_instance->rconn_intv = 1;
      my_instance->port = 5672;
      my_instance->trgtype = TRG_ALL;
      my_instance->log_all = false;
      my_instance->strict_logging = true;

      for(i = 0;params[i];i++){
	if(!strcmp(params[i]->name,"hostname")){
	  my_instance->hostname = strdup(params[i]->value);
	}else if(!strcmp(params[i]->name,"username")){
	  my_instance->username = strdup(params[i]->value);
	}else if(!strcmp(params[i]->name,"password")){
	  my_instance->password = strdup(params[i]->value);
	}else if(!strcmp(params[i]->name,"vhost")){
	  my_instance->vhost = strdup(params[i]->value);
	}else if(!strcmp(params[i]->name,"port")){
	  my_instance->port = atoi(params[i]->value);
	}else if(!strcmp(params[i]->name,"exchange")){
	  my_instance->exchange = strdup(params[i]->value);  
	}else if(!strcmp(params[i]->name,"key")){
	  my_instance->key = strdup(params[i]->value);
	}else if(!strcmp(params[i]->name,"queue")){
	  my_instance->queue = strdup(params[i]->value);
	}
	else if(!strcmp(params[i]->name,"ssl_client_certificate")){

	  my_instance->ssl_client_cert = strdup(params[i]->value);
	
	}else if(!strcmp(params[i]->name,"ssl_client_key")){

	  my_instance->ssl_client_key = strdup(params[i]->value);
	  
	}else if(!strcmp(params[i]->name,"ssl_CA_cert")){

	  my_instance->ssl_CA_cert = strdup(params[i]->value);

	}else if(!strcmp(params[i]->name,"exchange_type")){

	  my_instance->exchange_type = strdup(params[i]->value);

	}else if(!strcmp(params[i]->name,"logging_trigger")){
	  
	  arr = parse_optstr(params[i]->value,",",&arrsize);

	  for(x = 0;x<arrsize;x++){
	    if(!strcmp(arr[x],"source")){
	      my_instance->trgtype |= TRG_SOURCE;
	    }else if(!strcmp(arr[x],"schema")){
	      my_instance->trgtype |= TRG_SCHEMA;
	    }else if(!strcmp(arr[x],"object")){
	      my_instance->trgtype |= TRG_OBJECT;
	    }else if(!strcmp(arr[x],"all")){
	      my_instance->trgtype = TRG_ALL;
	    }else{
	      skygw_log_write(LOGFILE_ERROR,"Error: Unknown option for 'logging_trigger':%s.",arr[x]);
	    }
	  }

	  if(arrsize > 0){
	    free(arr);
	  }
	  arrsize = 0;
	  


	}else if(strstr(params[i]->name,"logging_")){

	  if(paramcount < parammax){
	    paramlist[paramcount] = malloc(sizeof(FILTER_PARAMETER));
	    paramlist[paramcount]->name = strdup(params[i]->name);
	    paramlist[paramcount]->value = strdup(params[i]->value);
	    paramcount++;
	  }	  
	  
	}

      }

      if(my_instance->trgtype & TRG_SOURCE){

        my_instance->src_trg = (SRC_TRIG*)malloc(sizeof(SRC_TRIG));
        my_instance->src_trg->user = NULL;
        my_instance->src_trg->host = NULL;
        my_instance->src_trg->usize = 0;
	my_instance->src_trg->hsize = 0;	

      }

      if(my_instance->trgtype & TRG_SCHEMA){

        my_instance->shm_trg = (SHM_TRIG*)malloc(sizeof(SHM_TRIG));
        my_instance->shm_trg->objects = NULL;
        my_instance->shm_trg->size = 0;

      }

      if(my_instance->trgtype & TRG_OBJECT){

        my_instance->obj_trg = (OBJ_TRIG*)malloc(sizeof(OBJ_TRIG));
        my_instance->obj_trg->objects = NULL;
        my_instance->obj_trg->size = 0;

      }

      for(i = 0;i<paramcount;i++){

	if(!strcmp(paramlist[i]->name,"logging_source_user")){
	    
	  if(my_instance->src_trg){
	    my_instance->src_trg->user = parse_optstr(paramlist[i]->value,",",&arrsize);
	    my_instance->src_trg->usize = arrsize;
	    arrsize = 0;
	  }

	}else if(!strcmp(paramlist[i]->name,"logging_source_host")){
	    
	  if(my_instance->src_trg){
	    my_instance->src_trg->host = parse_optstr(paramlist[i]->value,",",&arrsize);
	    my_instance->src_trg->hsize = arrsize;
	    arrsize = 0;
	  }

	}else if(!strcmp(paramlist[i]->name,"logging_schema")){
	    
	  if(my_instance->shm_trg){
	    my_instance->shm_trg->objects = parse_optstr(paramlist[i]->value,",",&arrsize);
	    my_instance->shm_trg->size = arrsize;
	    arrsize = 0;
	  }

	}else if(!strcmp(paramlist[i]->name,"logging_object")){
	    
	  if(my_instance->obj_trg){
	    my_instance->obj_trg->objects = parse_optstr(paramlist[i]->value,",",&arrsize);
	    my_instance->obj_trg->size = arrsize;
	    arrsize = 0;
	  }

	}else if(!strcmp(paramlist[i]->name,"logging_log_all")){
	  if(config_truth_value(paramlist[i]->value)){
	    my_instance->log_all = true;
	  }
	}else if(!strcmp(paramlist[i]->name,"logging_strict")){
	  if(!config_truth_value(paramlist[i]->value)){
	    my_instance->strict_logging = false;
	  }
	}
	free(paramlist[i]->name);
	free(paramlist[i]->value);
	free(paramlist[i]);
      }

      free(paramlist);
      
      if(my_instance->hostname == NULL){
	my_instance->hostname = strdup("localhost");	
      }
      if(my_instance->username == NULL){
	my_instance->username = strdup("guest");	
      }
      if(my_instance->password == NULL){
	my_instance->password = strdup("guest");	
      }
      if(my_instance->vhost == NULL){
	my_instance->vhost = strdup("/");	
      }
      if(my_instance->exchange == NULL){
	my_instance->exchange = strdup("default_exchange");	
      }
      if(my_instance->key == NULL){
	my_instance->key = strdup("key");
      }
      if(my_instance->exchange_type == NULL){
	my_instance->exchange_type = strdup("direct");
      }

      if(my_instance->ssl_client_cert != NULL &&
	 my_instance->ssl_client_key != NULL &&
	 my_instance->ssl_CA_cert != NULL){
	my_instance->use_ssl = true;
      }else{
	my_instance->use_ssl = false;
      }
      
      if(my_instance->use_ssl){
	amqp_set_initialize_ssl_library(0);/**Assume the underlying SSL library is already initialized*/
      }
      
      /**Connect to the server*/
      init_conn(my_instance);

      snprintf(taskname,511,"mqtask%d",atomic_add(&hktask_id,1));
      hktask_add(taskname,sendMessage,(void*)my_instance,5);

    }
  return (FILTER *)my_instance;
}