Ejemplo n.º 1
0
static void ngx_proc_luashm_backup_exit_process(ngx_cycle_t *cycle)
{
    ngx_proc_luashm_backup_conf_t 		*pbcf;
	ngx_shm_zone_t   				*zone;
	ngx_str_t 						*name;
	ngx_uint_t 						i;

    pbcf = ngx_proc_get_conf(cycle->conf_ctx, ngx_proc_luashm_backup_module);
	if (pbcf == NULL) {
        return;
    }
	pbcf->running = 0;
    //ngx_close_socket(pbcf->fd);

	i = 0;
	for (; i < pbcf->shdict_names->nelts;i++)
	{
		name = ((ngx_str_t*)pbcf->shdict_names->elts)  + i;
		zone = ngx_http_lua_find_zone(name->data,name->len);
		if(zone != NULL){
			ngx_log_error(NGX_LOG_INFO,cycle->log,0,"shm.name:%s,shm.name_len:%l,shm.size:%l ", name->data,name->len,zone->shm.size);
			ngx_proc_luashm_backup_backup(cycle,zone);
		}
	}

	ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "exit luashm_backup %V",&ngx_cached_http_time);
}
Ejemplo n.º 2
0
	static void
ngx_proc_send_exit_process(ngx_cycle_t *cycle)
{
	ngx_proc_send_conf_t *pbcf;

	pbcf = ngx_proc_get_conf(cycle->conf_ctx, ngx_proc_send_module);

	ngx_close_socket(pbcf->fd);
	munmap(read_write_mmap, sizeof(long)*3);
	munmap(write_file, pbcf->mmap_dat_size);
}
Ejemplo n.º 3
0
static void ngx_proc_luashm_backup_thread_cycle(void *data)
{
	ngx_proc_luashm_backup_conf_t  	*pbcf;
    ngx_cycle_t 					*cycle;
    ngx_shm_zone_t   				*zone;
	ngx_str_t 						*name;
	ngx_uint_t 						i;
	ngx_time_t                  	*tp;
    int64_t                     	now,settimestamp;

	cycle = data;
	pbcf = ngx_proc_get_conf(cycle->conf_ctx, ngx_proc_luashm_backup_module);
	if(pbcf->settimestamp.data == NULL){
		pthread_exit((void *)0);
		return;
	}

	while(pbcf->running){
		ngx_time_update();
		if(ngx_strstr(pbcf->settimestamp.data,":")==NULL){
			settimestamp=ngx_atoi(pbcf->settimestamp.data,pbcf->settimestamp.len);
		}else{
			tp = ngx_timeofday();
	    	now = (int64_t) tp->sec * 1000 + tp->msec;
			settimestamp=GetTick((char*)pbcf->settimestamp.data)*1000;
			ngx_log_error(NGX_LOG_DEBUG, ngx_cycle->log, 0, "========= now:%lu, settimestamp:%lu =========",now,settimestamp);
			if(settimestamp<now)
				settimestamp=(24*60*60*1000)-(now-settimestamp);
			else
				settimestamp=settimestamp-now;

			if(settimestamp==0)settimestamp=1;
		}
		ngx_log_error(NGX_LOG_DEBUG, ngx_cycle->log, 0, "========= backup time:%s, sleeptime:%lu =========",pbcf->settimestamp.data,settimestamp);

		ngx_msleep(settimestamp);
		if(!pbcf->running)break;

		i = 0;
		for (; i < pbcf->shdict_names->nelts;i++)
		{
			name = ((ngx_str_t*)pbcf->shdict_names->elts)  + i;
			zone = ngx_http_lua_find_zone(name->data,name->len);
			dd("shm.name:%s,shm.name_len:%ld,shm.size:%ld  ", name->data,name->len,zone->shm.size);
			ngx_proc_luashm_backup_backup(cycle,zone);
		}
		ngx_log_error(NGX_LOG_DEBUG, cycle->log, 0, "luashm_backup %V",&ngx_cached_http_time);
		ngx_sleep(60);
		if(!pbcf->running)break;

	}

	pthread_exit((void *)0);
}
Ejemplo n.º 4
0
	static ngx_int_t
ngx_proc_send_prepare(ngx_cycle_t *cycle)
{
	ngx_proc_send_conf_t  *pbcf;

	pbcf = ngx_proc_get_conf(cycle->conf_ctx, ngx_proc_send_module);
	if (!pbcf->enable) {
		return NGX_DECLINED;
	}

	if (pbcf->port == 0) {
		return NGX_DECLINED;
	}

	return NGX_OK;
}
Ejemplo n.º 5
0
static ngx_int_t ngx_proc_luashm_backup_prepare(ngx_cycle_t *cycle)
{
    ngx_proc_luashm_backup_conf_t  *pbcf;

    pbcf = ngx_proc_get_conf(cycle->conf_ctx, ngx_proc_luashm_backup_module);

	if (pbcf == NULL) {
        return NGX_DECLINED;
    }
	if (pbcf->path.data == NULL) {
        return NGX_DECLINED;
    }
	if (pbcf->shdict_names->nelts == 0) {
        return NGX_DECLINED;
    }
    return NGX_OK;
}
Ejemplo n.º 6
0
void* send_info(void* arg){
	ngx_cycle_t *cycle= (ngx_cycle_t*)(arg);
	ngx_proc_send_conf_t  *pbcf;
	pbcf = ngx_proc_get_conf(cycle->conf_ctx, ngx_proc_send_module);

	open_fifo(cycle);
	while(1){
		if(start != end){
			int p = start;
			char out[SENDOUTBUFSIZE + 100];
			memset(out, 0, SENDOUTBUFSIZE + 100);
			sprintf(out, "%s\n", buf[p]);
			if(__sync_bool_compare_and_swap(&start, p, (p + 1) % BUFSIZE)){
				if(*(read_write_mmap + 1) >= *(read_write_mmap + 2) && *(read_write_mmap + 1) + (long)strlen(out) < (long)(pbcf->mmap_dat_size)){
					strcpy(write_file + *(read_write_mmap + 1), out);
					*(read_write_mmap + 1) = *(read_write_mmap + 1) + strlen(out);
				}
				else if(*(read_write_mmap + 1) >= *(read_write_mmap + 2) && *(read_write_mmap + 1) + (long)strlen(out) >= (long)(pbcf->mmap_dat_size)){
					if(((long)strlen(out) - ((long)(pbcf->mmap_dat_size) - *(read_write_mmap + 1)))< *(read_write_mmap + 2)){
						strncpy(write_file + *(read_write_mmap + 1), out, (long)(pbcf->mmap_dat_size) - *(read_write_mmap + 1));
						strcpy(write_file, out + (long)(pbcf->mmap_dat_size) - *(read_write_mmap + 1));
						*(read_write_mmap + 1) = strlen(out) - ((long)(pbcf->mmap_dat_size) - *(read_write_mmap + 1));
					}
				}
				else if(*(read_write_mmap + 1) < *(read_write_mmap + 2) && *(read_write_mmap + 1) + (long)strlen(out) < *(read_write_mmap + 2)){
					strcpy(write_file + *(read_write_mmap + 1), out);
					*(read_write_mmap + 1) = *(read_write_mmap + 1) + strlen(out);

				}
			}
		}
		else{
			usleep(10000);
		}
	}
	return NULL;
}
Ejemplo n.º 7
0
int open_fifo(ngx_cycle_t *cycle){

	ngx_proc_send_conf_t  *pbcf;
	pbcf = ngx_proc_get_conf(cycle->conf_ctx, ngx_proc_send_module);

	int fd = 0;
	int off_fd = 0;
	createdir((char*)pbcf->mmap_idx.data, cycle);
	createdir((char *)pbcf->mmap_dat.data, cycle);


	if(access((char*)pbcf->mmap_idx.data, 0) == -1){
		off_fd = open((char*)pbcf->mmap_idx.data, O_CREAT|O_RDWR, 0777);
		if(off_fd == -1){
			ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "create file %V failed", pbcf->mmap_idx);
			return -1;
		}
		lseek(off_fd, sizeof(long)*3-1, SEEK_SET);
		write(off_fd,"",1);
		read_write_mmap = (long*) mmap( NULL,sizeof(long)*3, PROT_READ|PROT_WRITE, MAP_SHARED, off_fd, 0);
		*read_write_mmap = (unsigned int)pbcf->mmap_dat_size;
		*(read_write_mmap + 1) = 0;
		*(read_write_mmap + 2) = 0;
	}
	else{
		off_fd = open((char*)pbcf->mmap_idx.data, O_CREAT|O_RDWR, 0777);
		if(off_fd == -1){
			ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "open file %V failed", pbcf->mmap_idx);
			return -1;
		}
		if(get_file_size((char*)pbcf->mmap_idx.data) < (long)sizeof(long)*3){
			lseek(off_fd, sizeof(long)*3-1, SEEK_SET);
			write(off_fd,"",1);
			read_write_mmap = (long*) mmap( NULL,sizeof(long)*2, PROT_READ|PROT_WRITE, MAP_SHARED, off_fd, 0);
			*read_write_mmap = (unsigned int)pbcf->mmap_dat_size;
			*(read_write_mmap + 1) = 0;
			*(read_write_mmap + 2) = 0;
		}
		read_write_mmap = (long*) mmap( NULL,sizeof(long)*3, PROT_READ|PROT_WRITE, MAP_SHARED, off_fd, 0);
		if( *read_write_mmap != (long)pbcf->mmap_dat_size ){
			*read_write_mmap = (unsigned int)pbcf->mmap_dat_size;
			*(read_write_mmap + 1) = 0;
			*(read_write_mmap + 2) = 0;
		}
	}
	close(off_fd);

	if(access((char *)pbcf->mmap_dat.data, 0) == -1){
		fd = open((char *)pbcf->mmap_dat.data, O_CREAT|O_RDWR, 0777);
		if(fd == -1){
			ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "open file %V failed", pbcf->mmap_dat);
			return -1;
		}
		lseek(fd, pbcf->mmap_dat_size - 1, SEEK_SET);
		write(fd,"",1);
		write_file = (char*) mmap( NULL, pbcf->mmap_dat_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
	}
	else{
		fd = open((char *)pbcf->mmap_dat.data, O_CREAT|O_RDWR, 0777);
		if(fd == -1){
			ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "open file %V failed", pbcf->mmap_dat);
			return -1;
		}
		if(get_file_size((char *)pbcf->mmap_dat.data) < (long)pbcf->mmap_dat_size){
			lseek(fd, pbcf->mmap_dat_size - 1, SEEK_SET);
			write(off_fd,"",1);
			read_write_mmap = (long*) mmap(NULL, pbcf->mmap_dat_size, PROT_READ|PROT_WRITE, MAP_SHARED, off_fd, 0);
		}
		write_file = (char*) mmap(NULL, pbcf->mmap_dat_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
	}

	close(fd);
	return 0;
}
Ejemplo n.º 8
0
	static ngx_int_t
ngx_proc_send_process_init(ngx_cycle_t *cycle)
{
	int                       reuseaddr;
	ngx_event_t              *rev;
	ngx_socket_t              fd;
	ngx_connection_t         *c;
	struct sockaddr_in        sin;
	ngx_proc_send_conf_t  *pbcf;

	pbcf = ngx_proc_get_conf(cycle->conf_ctx, ngx_proc_send_module);
	fd = ngx_socket(AF_INET, SOCK_STREAM, 0);
	if (fd == -1) {
		ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "send socket error");
		return NGX_ERROR;
	}

	reuseaddr = 1;

	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
				(const void *) &reuseaddr, sizeof(int))
			== -1)
	{
		ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
				"send setsockopt(SO_REUSEADDR) failed");

		ngx_close_socket(fd);
		return NGX_ERROR;
	}
	if (ngx_nonblocking(fd) == -1) {
		ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
				"send nonblocking failed");

		ngx_close_socket(fd);
		return NGX_ERROR;
	}

	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = htonl(INADDR_ANY);
	sin.sin_port = htons(pbcf->port);

	if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
		ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "send bind error");
		return NGX_ERROR;
	}

	if (listen(fd, 20) == -1) {
		ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "send listen error");
		return NGX_ERROR;
	}

	c = ngx_get_connection(fd, cycle->log);
	if (c == NULL) {
		ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "send no connection");
		return NGX_ERROR;
	}

	c->log = cycle->log;
	rev = c->read;
	rev->log = c->log;
	rev->accept = 1;
	rev->handler = ngx_proc_send_accept;

	if (ngx_add_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
		return NGX_ERROR;
	}

	pbcf->fd = fd;

	return NGX_OK;
}
Ejemplo n.º 9
0
static ngx_int_t ngx_proc_luashm_backup_process_init(ngx_cycle_t *cycle)
{
    ngx_proc_luashm_backup_conf_t  		*pbcf;
	ssize_t							n;
	ngx_uint_t						i;
	ngx_http_lua_shdict_node_ext_t  *extsd;
	ngx_shm_zone_t              	*zone;
	ngx_http_lua_shdict_ctx_t   	*sdctx;
	ngx_file_t 		 				file;
	ngx_str_t  						*name;
	ngx_buf_t 						*buff;
	u_char							*shm_file_path;
	ngx_time_t                  	*tp;
    uint64_t                     	now;
	uint32_t						content_size;

    pbcf = ngx_proc_get_conf(cycle->conf_ctx, ngx_proc_luashm_backup_module);
	if (pbcf == NULL) {
        return NGX_ERROR;
    }
	pbcf->running=1;

	tp = ngx_timeofday();
    now = (uint64_t) tp->sec * 1000 + tp->msec;

	i = 0;
	for (; i < pbcf->shdict_names->nelts;i++)
	{
		name = ((ngx_str_t*)pbcf->shdict_names->elts)  + i;
		zone = ngx_http_lua_find_zone(name->data,name->len);
		dd("shm.name:%s,shm.name_len:%ld,shm.size:%ld  ", name->data,name->len,zone->shm.size);
		sdctx = zone->data;

		shm_file_path=ngx_palloc(cycle->pool,pbcf->path.len + 5+ zone->shm.name.len);
		ngx_sprintf(shm_file_path,"%s/%s.dat",pbcf->path.data,zone->shm.name.data);

		file.fd = open((char*)shm_file_path, O_RDONLY|O_CREAT, S_IWUSR|S_IWGRP|S_IRGRP|S_IRUSR);
		if (file.fd == NGX_INVALID_FILE) {
			//ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,ngx_open_file_n " \"%s\" failed",filename->data);
			ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,ngx_open_file_n " \"%s\" failed",shm_file_path);
			ngx_pfree(cycle->pool,shm_file_path);
			return NGX_ERROR;
		}
		file.name.len = pbcf->path.len;
		file.name.data = pbcf->path.data;
		file.offset = 0;
		file.log = cycle->log;
		file.offset=0;

		buff = ngx_create_temp_buf(cycle->pool, zone->shm.size+65536);
		n = ngx_read_file(&file,buff->pos,buff->end-buff->start,0);
		ngx_close_file(file.fd);
		if(n>0){
			ngx_memcpy(&content_size,buff->pos,sizeof(uint32_t));
			ngx_log_error(NGX_LOG_INFO,cycle->log,0,"read file:%s, content_size:%l bytes, file_size:%z bytes, nowtime:%l", shm_file_path,content_size,n,now);
			buff->last = content_size==n?(buff->last+n):(content_size>n?0:buff->last+content_size);
			buff->pos += sizeof(uint32_t);
			while(buff->last - buff->pos >0){
				extsd=(ngx_http_lua_shdict_node_ext_t*)buff->pos;
				if(extsd->expires == 0 || ((int64_t)(extsd->expires-now) > 60000))
					ngx_proc_luashm_backup_recover(sdctx,extsd);

				buff->pos+=sizeof(ngx_http_lua_shdict_node_ext_t)+extsd->key_len+extsd->value_len;
			}
		}
		ngx_pfree(cycle->pool,shm_file_path);
		ngx_pfree(cycle->pool,buff->start);
		ngx_pfree(cycle->pool,buff);
	}

    return NGX_OK;
}
Ejemplo n.º 10
0
static int ngx_proc_luashm_backup_backup(ngx_cycle_t *cycle,ngx_shm_zone_t *zone)
{
	ngx_proc_luashm_backup_conf_t  	*pbcf;
    ngx_queue_t                 	*q, *prev;
    ngx_http_lua_shdict_node_t  	*sd;
	ngx_http_lua_shdict_node_ext_t 	*extsd;
    ngx_http_lua_shdict_ctx_t   	*ctx;
    ngx_time_t                  	*tp;
    int                          	total = 0;
    uint64_t                     	now;
	uint32_t						content_size;
	size_t                       	len;
	ngx_buf_t 						*buff;
	double							*num_value;
	ngx_file_t 		 				file;
	u_char							*shm_file_path;

	pbcf = ngx_proc_get_conf(cycle->conf_ctx, ngx_proc_luashm_backup_module);


    ctx = zone->data;
	buff = ngx_create_temp_buf(cycle->pool, zone->shm.size+65536);
	buff->last += sizeof(uint32_t);

	tp = ngx_timeofday();

    now = (uint64_t) tp->sec * 1000 + tp->msec;

    ngx_shmtx_lock(&ctx->shpool->mutex);

    if (ngx_queue_empty(&ctx->sh->queue)) {
        ngx_shmtx_unlock(&ctx->shpool->mutex);
        return 1;
    }

    /* first run through: get total number of elements we need to allocate */

    /* second run through: add keys to table */

    total = 0;
    q = ngx_queue_last(&ctx->sh->queue);

    while (q != ngx_queue_sentinel(&ctx->sh->queue)) {
        prev = ngx_queue_prev(q);

        sd = ngx_queue_data(q, ngx_http_lua_shdict_node_t, queue);

        if (sd->expires == 0 || ((int64_t)(sd->expires - now)) > 60000) {
            //lua_pushlstring(L, (char *) sd->data, sd->key_len);
            //lua_rawseti(L, -2, ++total);
			extsd = (ngx_http_lua_shdict_node_ext_t*)buff->last;
			extsd->key_len=sd->key_len;
			extsd->expires=sd->expires;
			extsd->value_type=sd->value_type;
			extsd->user_flags=sd->user_flags;

			len = sd->key_len+sd->value_len;
			ngx_memcpy(extsd->data,sd->data,len);
			buff->last += sizeof(ngx_http_lua_shdict_node_ext_t)+len;

			switch (sd->value_type) {
			case LUA_TSTRING:
				extsd->value_len=sd->value_len;
				break;
			case LUA_TNUMBER:
				extsd->value_len=sizeof(double);
				num_value = extsd->data+extsd->key_len;
				//ngx_memcpy(num_value, extsd->data+extsd->key_len, extsd->value_len);
				break;
			case LUA_TBOOLEAN:
				extsd->value_len=sizeof(u_char);
				break;
			}
			//dd("key:%s, key_len:%d,value:%f,value_len:%d,expires:%lu,now:%lu",extsd->data,extsd->key_len,*num_value,extsd->value_len,extsd->expires,now);

        }

        q = prev;
    }

    ngx_shmtx_unlock(&ctx->shpool->mutex);

	shm_file_path=ngx_palloc(cycle->pool,pbcf->path.len + 5+ zone->shm.name.len);
	ngx_sprintf(shm_file_path,"%s/%s.dat",pbcf->path.data,zone->shm.name.data);
	content_size = buff->last-buff->pos;
	ngx_log_error(NGX_LOG_INFO,cycle->log,0,"save file:%s, content_size:%l bytes, nowtime:%l", shm_file_path,content_size,now);
	if(content_size > sizeof(uint32_t))
		ngx_memcpy(buff->pos,&content_size,sizeof(uint32_t));
	file.fd = open((char*)shm_file_path, O_TRUNC|O_RDWR, S_IWUSR|S_IWGRP|S_IRGRP|S_IRUSR);
	if (file.fd == NGX_INVALID_FILE) {
		//ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,ngx_open_file_n " \"%s\" failed",filename->data);
		ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,ngx_open_file_n " \"%s\" failed",shm_file_path);
		ngx_pfree(cycle->pool,buff);
		ngx_pfree(cycle->pool,shm_file_path);
		return NGX_ERROR;
	}
	file.name.len = pbcf->path.len;
	file.name.data = pbcf->path.data;
	file.offset = 0;
	file.log = cycle->log;
	file.offset=0;
	ngx_write_file(&file,buff->pos,content_size,0);
	ngx_close_file(file.fd);

    /* table is at top of stack */
	ngx_pfree(cycle->pool,shm_file_path);
	ngx_pfree(cycle->pool,buff->start);
	ngx_pfree(cycle->pool,buff);
    return 1;
}