Commit 4631a207 authored by Yoshinori Okuji's avatar Yoshinori Okuji

Correct the implementation of generate_tid.

git-svn-id: https://svn.erp5.org/repos/neo/trunk@3 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent bbb45f89
#ifndef timestamp_h #ifndef timestamp_h
#define timestamp_h #define timestamp_h
int generate_tid (u_int16_t ntid[ID_LEN], u_int16_t ltid[ID_LEN]); int generate_tid (unsigned char ntid[ID_LEN], unsigned char ltid[ID_LEN]);
#endif #endif
#include <sys/time.h>
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include <netinet/in.h>
#include "neo_struct.h" #include "neo_struct.h"
/* int */
/* inc_tid (u_int16_t tid[ID_LEN], int pos) */
/* { */
/* /\* printf ("inc_tid : pos %d, %d\n", pos, tid[pos]); *\/ */
/* tid[pos]++; */
/* /\* printf ("inc %d\n", tid[pos]); *\/ */
/* if (tid[pos] > 255) */
/* { */
/* tid[pos] = 0; */
/* if (pos >= 0) */
/* inc_tid (tid, pos - 1); */
/* else */
/* { */
/* printf ("no more tid left to generate\n"); */
/* return 0; */
/* } */
/* } */
/* return 1; */
/* } */
int int
generate_tid (u_int16_t ntid[ID_LEN], u_int16_t ltid[ID_LEN]) generate_tid (unsigned char ntid[ID_LEN], unsigned char ltid[ID_LEN])
{ {
int i; struct timeval tv;
time_t epoch_time; struct tm tm;
struct tm *gm_time;
u_int32_t v; u_int32_t v;
double sconv = 0; double sec;
unsigned char new_tid[ID_LEN], last_tid[ID_LEN];
/* generate time */ /* generate time */
time (&epoch_time); if (gettimeofday (&tv, 0) < 0)
gm_time = (struct tm *) malloc (sizeof (struct tm)); return 0;
gm_time = gmtime (&epoch_time);
if (! gmtime_r (&tv.tv_sec, &tm))
return 0;
/* create an integer array */ /* create an integer array */
v = v =
((((gm_time->tm_year) * 12 + gm_time->tm_mon) * 31 + ((((tm.tm_year) * 12 + tm.tm_mon) * 31 +
gm_time->tm_mday - 1) * 24 + gm_time->tm_hour) * 60 + gm_time->tm_min; tm.tm_mday - 1) * 24 + tm.tm_hour) * 60 + tm.tm_min;
ntid[0] = v / 16777216; v = htonl (v);
ntid[1] = (v % 16777216) / 65536; memcpy (ntid, &v, 4);
ntid[2] = (v % 65536) / 256; sec = (tv.tv_sec % 60) + ((double) tv.tv_usec / 1000000);
ntid[3] = v % 256; sec /= ((double) 60) / ((double) (1 << 16)) / ((double) (1 << 16));
gm_time->tm_sec /= sconv; v = (u_int32_t) sec;
v = (u_int32_t) epoch_time % 60; v = htonl (v);
ntid[4] = v / 16777216; memcpy (ntid + 4, &v, 4);
ntid[5] = (v % 16777216) / 65536;
ntid[6] = (v % 65536) / 256; while (memcmp (ntid, ltid, ID_LEN) <= 0)
ntid[7] = v % 256;
compare:
/* create the string array */
for (i = 0; i < ID_LEN; i++)
{ {
new_tid[i] = (unsigned char)ntid[i]; int i;
last_tid[i] = (unsigned char)ltid[i];
}
if (memcmp (new_tid, last_tid, ID_LEN) > 0)
return 1;
for (i = ID_LEN - 1; i > 3; i--) for (i = ID_LEN - 1; i > 3; i--)
{ {
...@@ -77,10 +44,12 @@ generate_tid (u_int16_t ntid[ID_LEN], u_int16_t ltid[ID_LEN]) ...@@ -77,10 +44,12 @@ generate_tid (u_int16_t ntid[ID_LEN], u_int16_t ltid[ID_LEN])
else else
{ {
ntid[i]++; ntid[i]++;
goto compare; continue;
} }
} }
printf ("oups must finish compare function...\n");
return 0; return 0;
}
return 1;
} }
...@@ -39,7 +39,7 @@ static int soc = -1; ...@@ -39,7 +39,7 @@ static int soc = -1;
static uuid_t master_id; static uuid_t master_id;
static char ID[UUID_LEN+1]; /* uuid is unparse into a 36 byte string */ static char ID[UUID_LEN+1]; /* uuid is unparse into a 36 byte string */
static u_int64_t ltid; /* store last transaction id */ static u_int64_t ltid; /* store last transaction id */
static u_int16_t last_generated_tid[ID_LEN]; /* last generated tid, use to check if tid increases */ static unsigned char last_generated_tid[ID_LEN]; /* last generated tid, use to check if tid increases */
static u_int16_t replication_nb = DEFAULT_REPLICATION_NB; /* number of replication for a transaction on storages nodes */ static u_int16_t replication_nb = DEFAULT_REPLICATION_NB; /* number of replication for a transaction on storages nodes */
static u_int16_t last_storage_replication; /* id of the last storage node used for replication */ static u_int16_t last_storage_replication; /* id of the last storage node used for replication */
static struct storageInfo DBinfo; /* store informations about storage system */ static struct storageInfo DBinfo; /* store informations about storage system */
...@@ -120,7 +120,7 @@ cleanup_handler (int sig) ...@@ -120,7 +120,7 @@ cleanup_handler (int sig)
/* must store last oid and tid */ /* must store last oid and tid */
fd = fopen (MASTER_DATA_FILE, "w"); fd = fopen (MASTER_DATA_FILE, "w");
fwrite (ascii, sizeof (int), 8, fd); fwrite (ascii, sizeof (int), 8, fd);
fwrite (last_generated_tid, sizeof (u_int16_t), 8, fd); fwrite (last_generated_tid, sizeof (unsigned char), 8, fd);
fclose (fd); fclose (fd);
/* send message of master close to all storages first */ /* send message of master close to all storages first */
...@@ -721,7 +721,7 @@ h_beginTrans (int conn, char *hbuf) ...@@ -721,7 +721,7 @@ h_beginTrans (int conn, char *hbuf)
u_int64_t tid; u_int64_t tid;
u_int32_t len = 0, data_len = 0, i; u_int32_t len = 0, data_len = 0, i;
char rflags[FLAG_LEN], *buf = NULL, *id = NULL; char rflags[FLAG_LEN], *buf = NULL, *id = NULL;
u_int16_t s[ID_LEN]; unsigned char s[ID_LEN];
struct stringList storages; struct stringList storages;
/* header */ /* header */
...@@ -1817,7 +1817,7 @@ main (int argc, char **argv) ...@@ -1817,7 +1817,7 @@ main (int argc, char **argv)
{ {
fd = fopen (MASTER_DATA_FILE, "r"); fd = fopen (MASTER_DATA_FILE, "r");
fread (ascii, sizeof (int), 8, fd); fread (ascii, sizeof (int), 8, fd);
fread (last_generated_tid, sizeof (u_int16_t), 8, fd); fread (last_generated_tid, sizeof (unsigned char), 8, fd);
fclose (fd); fclose (fd);
} }
else else
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment