summaryrefslogtreecommitdiffstats
path: root/lib/clplumbing/cl_compress.c
blob: 6b56ad63af161b68119578423370499912e4096d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
 * compress.c: Compression functions for Linux-HA
 *
 * Copyright (C) 2005 Guochun Shi <gshi@ncsa.uiuc.edu>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * Compression is designed to handle big messages, right now with 4 nodes
 * cib message can go up to 64 KB or more. I expect much larger messages
 * when the number of node increase. This makes message compression necessary.
 *
 *
 * Compression is handled in field level. One can add a struct field using
 * ha_msg_addstruct() -- the field will not get compressed, or using 
 * ha_msg_addstruct_compress(), and the field will get compressed when
 * the message is converted to wire format, i.e. when msg2wirefmt() is called.
 * The compressed field will stay compressed until it reached the desination.
 * It will finally decompressed when the user start to get the field value.
 * It is designed this way so that the compression/decompression only happens
 * in end users so that heartbeat itself can save cpu cycle and memory.
 * (more info about compression can be found in cl_msg_types.c about FT_COMPRESS
 * FT_UNCOMPRESS types)
 *
 * compression has another legacy mode, which is there so it can be compatible 
 * to old ways of compression. In the old way, no field is compressed individually
 * and the messages is compressed before it is sent out, and it will be decompressed
 * in the receiver side immediately. So in each IPC channel, the message is compressed
 * and decompressed once. This way will cost a lot of cpu time and memory and it is 
 * discouraged.
 *
 * If use_traditional_compression is true, then it is using the legacy mode, otherwise
 * it is using the new compression. For back compatibility, the default is legacy mode.
 *
 * The real compression work is done by compression plugins. There are two plugins right
 * now: zlib and bz2, they are in lib/plugins/compress
 *
 */

#include <lha_internal.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <unistd.h>
#include <assert.h>
#include <glib.h>
#include <compress.h>
#include <ha_msg.h>
#include <clplumbing/netstring.h>
#include <pils/plugin.h>
#include <pils/generic.h>
#include <stonith/stonith.h>
#include <stonith/stonith_plugin.h>

#define COMPRESSED_FIELD "_compressed_payload"
#define COMPRESS_NAME "_compression_algorithm"
#define HACOMPRESSNAME "HA_COMPRESSION"
#define DFLT_COMPRESS_PLUGIN "bz2"

static struct hb_compress_fns* msg_compress_fns = NULL;
static char*  compress_name = NULL;
GHashTable*		CompressFuncs = NULL;

static PILGenericIfMgmtRqst	Reqs[] =
	{
		{"compress", &CompressFuncs, NULL, NULL, NULL},
		{NULL, NULL, NULL, NULL, NULL}
	};

static PILPluginUniv*		CompressPIsys = NULL;

static int
init_pluginsys(void){
	
	if (CompressPIsys) {
		return TRUE;
	}

	CompressPIsys = NewPILPluginUniv(HA_PLUGIN_DIR);
	
	if (CompressPIsys) {
		if (PILLoadPlugin(CompressPIsys, PI_IFMANAGER, "generic", Reqs)
		!=	PIL_OK){
			cl_log(LOG_ERR, "generic plugin load failed\n");
			DelPILPluginUniv(CompressPIsys);
			CompressPIsys = NULL;
		}
	}else{
		cl_log(LOG_ERR, "pi univ creation failed\n");
	}
	return CompressPIsys != NULL;

}

int
cl_compress_remove_plugin(const char* pluginname)
{
	return HA_OK;
}

int
cl_compress_load_plugin(const char* pluginname)
{
	struct hb_compress_fns*	funcs = NULL;

	if (!init_pluginsys()){
		return HA_FAIL;
	}
	
	if ((funcs = g_hash_table_lookup(CompressFuncs, pluginname))
	    == NULL){
		if (PILPluginExists(CompressPIsys, HB_COMPRESS_TYPE_S,
				    pluginname) == PIL_OK){
			PIL_rc rc;
			if ((rc = PILLoadPlugin(CompressPIsys,
						HB_COMPRESS_TYPE_S, 
						pluginname,
						NULL))!= PIL_OK){
				cl_log(LOG_ERR, 
				       "Cannot load compress plugin %s[%s]",
				       pluginname, 
				       PIL_strerror(rc));
				return HA_FAIL;
			}
			funcs = g_hash_table_lookup(CompressFuncs, 
						    pluginname);
		}
		
	}
	if (funcs == NULL){
		cl_log(LOG_ERR, "Compression module(%s) not found", pluginname);
		return HA_FAIL;
	}

	/* set the environment variable so that later programs can
	 * load the appropriate plugin
	 */
	setenv(HACOMPRESSNAME,pluginname,1);
	msg_compress_fns = funcs;
	
	return HA_OK;
}

int
cl_set_compress_fns(const char* pluginname)
{
	/* this function was unnecessary duplication of the
	 * code in cl_compress_load_plugin
	 */
	return cl_compress_load_plugin(pluginname);
}

struct hb_compress_fns*
cl_get_compress_fns(void)
{
	static int try_dflt = 1;

	if (try_dflt && !msg_compress_fns) {
		try_dflt = 0;
		cl_log(LOG_INFO, "%s: user didn't set compression type, "
		       "loading %s plugin",
		       __FUNCTION__, DFLT_COMPRESS_PLUGIN);
		cl_compress_load_plugin(DFLT_COMPRESS_PLUGIN);
	}
	return msg_compress_fns;
}

static struct hb_compress_fns*
get_compress_fns(const char* pluginname)
{
	struct hb_compress_fns*	funcs = NULL;
	
	if (cl_compress_load_plugin(pluginname) != HA_OK){
		cl_log(LOG_ERR, "%s: loading compression module"
		       "(%s) failed",
		       __FUNCTION__, pluginname);
		return NULL;
	}
	
	funcs = g_hash_table_lookup(CompressFuncs, pluginname);      
	return funcs;	
}

void cl_realtime_malloc_check(void);

char* 
cl_compressmsg(struct ha_msg* m, size_t* len)
{
	char*	src;
	char*	dest;
	size_t	destlen;
	int rc;
	char* ret = NULL;
	struct ha_msg* tmpmsg;
	size_t datalen;

	destlen = MAXMSG;

	dest = malloc(destlen);
	if (!dest) {
		cl_log(LOG_ERR, "%s: failed to allocate destination buffer",
		       __FUNCTION__);
		return NULL;
	}

	if (msg_compress_fns == NULL){
		cl_log(LOG_ERR, "%s: msg_compress_fns is NULL!",
		       __FUNCTION__);
		goto out;
	}
	if ( get_netstringlen(m) > MAXUNCOMPRESSED
	     || get_stringlen(m) > MAXUNCOMPRESSED){
		cl_log(LOG_ERR, "%s: msg too big(stringlen=%d,"
		       "netstringlen=%d)", 
		       __FUNCTION__, 
		       get_stringlen(m),
		       get_netstringlen(m));
		goto out;
	}
	
	
	if ((src = msg2wirefmt_noac(m, &datalen)) == NULL){
		cl_log(LOG_ERR,"%s: converting msg"
		       " to wirefmt failed", __FUNCTION__);
		goto out;
	}
	
	rc = msg_compress_fns->compress(dest, &destlen, 
					src, datalen);
	if (rc != HA_OK){
		cl_log(LOG_ERR, "%s: compression failed",
		       __FUNCTION__);
		goto out;
	}
	
	free(src);

	tmpmsg =ha_msg_new(0);
	rc = ha_msg_addbin(tmpmsg, COMPRESSED_FIELD, dest, destlen)/*discouraged function*/;
	
	if (rc != HA_OK){
		cl_log(LOG_ERR, "%s: adding binary to msg failed",
		       __FUNCTION__);
		goto out;
	}

	rc = ha_msg_add(tmpmsg, COMPRESS_NAME, 
			msg_compress_fns->getname());
	
	if (rc != HA_OK){
		cl_log(LOG_ERR, "%s: adding compress name to msg failed",
		       __FUNCTION__);
		goto out;
	}
	

	ret = msg2netstring(tmpmsg, len);
	ha_msg_del(tmpmsg);
	
#if 0
	cl_log(LOG_INFO, "------original stringlen=%d, netstringlen=%d,"
	       "compressed_datalen=%d,current len=%d",
	       get_stringlen(m), get_netstringlen(m),(int)destlen,  (int)*len);
	
#endif

out:
	if (dest) {
		free(dest);
	}
	
	return ret;
}


gboolean 
is_compressed_msg(struct ha_msg* m)
{
	if( cl_get_binary(m, COMPRESSED_FIELD, NULL) /*discouraged function*/
	    != NULL){
		return TRUE;
	}

	return FALSE;
	
}

/* the decompressmsg function is not exactly the reverse
 * operation of compressmsg, it starts when the prorgram
 * detects there is compressed_field in a msg
 */

struct ha_msg*
cl_decompressmsg(struct ha_msg* m)
{
	const char* src;
	size_t srclen;
	char *dest = NULL;
	size_t destlen = MAXUNCOMPRESSED;
	int rc;
	struct ha_msg* ret = NULL;
	const char* decompress_name;
	struct hb_compress_fns* funcs = NULL;

	dest = malloc(destlen);
	
	if (!dest) {
		cl_log(LOG_ERR, "%s: Failed to allocate buffer.", __FUNCTION__);
		return NULL;
	}
	
	if (m == NULL){
		cl_log(LOG_ERR, "%s: NULL message", __FUNCTION__);
		goto out;
	}
	src = cl_get_binary(m, COMPRESSED_FIELD, &srclen)/*discouraged function*/;
	if (src == NULL){
		cl_log(LOG_ERR, "%s: compressed-field is NULL",
		       __FUNCTION__);
		goto out;
	}

	if (srclen > MAXMSG){
		cl_log(LOG_ERR, "%s: field too long(%d)", 
		       __FUNCTION__, (int)srclen);
		goto out;
	}
	
	decompress_name = ha_msg_value(m, COMPRESS_NAME);
	if (decompress_name == NULL){
		cl_log(LOG_ERR, "compress name not found");
		goto out;
	}

	
	funcs = get_compress_fns(decompress_name);
	
	if (funcs == NULL){
		cl_log(LOG_ERR, "%s: compress method(%s) is not"
		       " supported in this machine",		       
		       __FUNCTION__, decompress_name);
		goto out;
	}
	
	rc = funcs->decompress(dest, &destlen, src, srclen);
	
	if (rc != HA_OK){
		cl_log(LOG_ERR, "%s: decompression failed",
		       __FUNCTION__);
		goto out;
	}
	
	ret = wirefmt2msg(dest, destlen, 0);	
	
#if 0
	cl_log(LOG_INFO, "%s: srclen =%d, destlen=%d", 
	       __FUNCTION__, 
	       srclen, destlen);
#endif

out:
	if (dest) {
		free(dest);
	}
	
	return ret;
}


int
cl_decompress_field(struct ha_msg* msg, int index, char* buf, size_t* buflen)
{
	char*		value;
	int		vallen;
	int		rc;
	const char*	decompress_name;
	struct hb_compress_fns* funcs;
	
	if ( msg == NULL|| index >= msg->nfields){
		cl_log(LOG_ERR, "%s: wrong argument",
		       __FUNCTION__);
		return HA_FAIL;
	}
	
	value = msg->values[index];
	vallen = msg->vlens[index];	
	
	decompress_name = ha_msg_value(msg, COMPRESS_NAME);
	if (decompress_name == NULL){
		cl_log(LOG_ERR, "compress name not found");
		return HA_FAIL;
	}
	
	
	funcs = get_compress_fns(decompress_name);
	
	if (funcs == NULL){
		cl_log(LOG_ERR, "%s: compress method(%s) is not"
		       " supported in this machine",		       
		       __FUNCTION__, decompress_name);
		return HA_FAIL;
	}
	
	rc = funcs->decompress(buf, buflen, value, vallen);		
	if (rc != HA_OK){
		cl_log(LOG_ERR, "%s: decompression failed",
		       __FUNCTION__);
		return HA_FAIL;
	}
	
	return HA_OK;
}


int 
cl_compress_field(struct ha_msg* msg, int index, char* buf, size_t* buflen)
{
	char*   src;
	size_t	srclen;
	int	rc;

	if ( msg == NULL|| index >= msg->nfields 
	     || msg->types[index] != FT_UNCOMPRESS){
		cl_log(LOG_ERR, "%s: wrong argument",
		       __FUNCTION__);
		return HA_FAIL;
	}
	
	if (msg_compress_fns == NULL){
		if (compress_name == NULL){
			compress_name = getenv(HACOMPRESSNAME);
		}
		
		if (compress_name == NULL){
			cl_log(LOG_ERR, "%s: no compression module name found",
			       __FUNCTION__);
			return HA_FAIL;			
		}

		if(cl_set_compress_fns(compress_name) != HA_OK){
			cl_log(LOG_ERR, "%s: loading compression module failed",
			       __FUNCTION__);
			return HA_FAIL;
		}
	}
	
	if (msg_compress_fns == NULL){
		cl_log(LOG_ERR, "%s: msg_compress_fns is NULL!",
		       __FUNCTION__);
		return HA_FAIL;
	}
	
	src = msg2wirefmt_noac(msg->values[index], &srclen);
	if (src == NULL){
		 cl_log(LOG_ERR,"%s: converting msg"
			" to wirefmt failed", __FUNCTION__);
		 return HA_FAIL;
	}
	
	rc = msg_compress_fns->compress(buf, buflen, 
					src, srclen);
	if (rc != HA_OK){
		cl_log(LOG_ERR, "%s: compression failed",
		       __FUNCTION__);
		return HA_FAIL;
	}
	
	
	rc = ha_msg_mod(msg, COMPRESS_NAME, 
			msg_compress_fns->getname());
	
	if (rc != HA_OK){
		cl_log(LOG_ERR, "%s: adding compress name to msg failed",
		       __FUNCTION__);
		return HA_FAIL;;
	}
	
	free(src);
	src = NULL;
	
	return HA_OK;
	
}