2016-09-13 18:54:26 -07:00
|
|
|
#ifndef COMMON_H
|
|
|
|
#define COMMON_H
|
|
|
|
|
2016-11-08 14:46:34 -08:00
|
|
|
#include <stdbool.h>
|
2016-09-23 22:53:58 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-09-18 18:06:42 -07:00
|
|
|
#include <string.h>
|
2016-09-13 18:54:26 -07:00
|
|
|
#include <errno.h>
|
2016-10-29 15:22:33 -07:00
|
|
|
#include <inttypes.h>
|
2016-11-22 17:04:24 -08:00
|
|
|
#ifndef _WIN32
|
2016-10-29 19:25:43 -07:00
|
|
|
#include <execinfo.h>
|
2016-11-22 17:04:24 -08:00
|
|
|
#endif
|
2016-09-13 18:54:26 -07:00
|
|
|
|
2016-11-06 17:31:14 -08:00
|
|
|
#include "utarray.h"
|
2017-03-05 02:05:02 -08:00
|
|
|
#ifdef __cplusplus
|
2017-04-07 12:32:12 -07:00
|
|
|
#include <functional>
|
2017-03-05 02:05:02 -08:00
|
|
|
extern "C" {
|
|
|
|
#endif
|
2016-12-08 20:57:08 -08:00
|
|
|
#include "sha256.h"
|
2017-03-05 02:05:02 -08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
2016-11-06 17:31:14 -08:00
|
|
|
|
2017-03-17 17:03:58 -07:00
|
|
|
/** The duration between heartbeats. These are sent by the plasma manager and
|
|
|
|
* local scheduler. */
|
|
|
|
#define HEARTBEAT_TIMEOUT_MILLISECONDS 100
|
|
|
|
/** If a component has not sent a heartbeat in the last NUM_HEARTBEATS_TIMEOUT
|
|
|
|
* heartbeat intervals, the global scheduler or monitor process will report it
|
|
|
|
* as dead to the db_client table. */
|
|
|
|
#define NUM_HEARTBEATS_TIMEOUT 100
|
|
|
|
|
2016-12-08 20:57:08 -08:00
|
|
|
/** Definitions for Ray logging levels. */
|
2016-11-15 20:33:29 -08:00
|
|
|
#define RAY_COMMON_DEBUG 0
|
|
|
|
#define RAY_COMMON_INFO 1
|
|
|
|
#define RAY_COMMON_WARNING 2
|
|
|
|
#define RAY_COMMON_ERROR 3
|
|
|
|
#define RAY_COMMON_FATAL 4
|
|
|
|
|
2016-12-08 20:57:08 -08:00
|
|
|
/**
|
|
|
|
* RAY_COMMON_LOG_LEVEL should be defined to one of the above logging level
|
|
|
|
* integer values. Any logging statement in the code with a logging level
|
|
|
|
* greater than or equal to RAY_COMMON_LOG_LEVEL will be outputted to stderr.
|
|
|
|
* The default logging level is INFO. */
|
2016-11-15 20:33:29 -08:00
|
|
|
#ifndef RAY_COMMON_LOG_LEVEL
|
|
|
|
#define RAY_COMMON_LOG_LEVEL RAY_COMMON_INFO
|
|
|
|
#endif
|
|
|
|
|
2017-03-20 16:31:46 -07:00
|
|
|
/* Arrow defines the same macro, only define it if it has not already been
|
|
|
|
* defined. */
|
|
|
|
#ifndef UNUSED
|
2016-12-22 03:11:46 -08:00
|
|
|
#define UNUSED(x) ((void) (x))
|
2017-03-20 16:31:46 -07:00
|
|
|
#endif
|
2016-12-22 03:11:46 -08:00
|
|
|
|
2016-12-08 20:57:08 -08:00
|
|
|
/**
|
|
|
|
* Macros to enable each level of Ray logging statements depending on the
|
|
|
|
* current logging level. */
|
2016-11-15 20:33:29 -08:00
|
|
|
#if (RAY_COMMON_LOG_LEVEL > RAY_COMMON_DEBUG)
|
2016-09-13 18:54:26 -07:00
|
|
|
#define LOG_DEBUG(M, ...)
|
|
|
|
#else
|
|
|
|
#define LOG_DEBUG(M, ...) \
|
|
|
|
fprintf(stderr, "[DEBUG] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
|
|
|
|
#endif
|
|
|
|
|
2016-11-15 20:33:29 -08:00
|
|
|
#if (RAY_COMMON_LOG_LEVEL > RAY_COMMON_INFO)
|
|
|
|
#define LOG_INFO(M, ...)
|
|
|
|
#else
|
2016-09-13 18:54:26 -07:00
|
|
|
#define LOG_INFO(M, ...) \
|
|
|
|
fprintf(stderr, "[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
|
2016-11-15 20:33:29 -08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (RAY_COMMON_LOG_LEVEL > RAY_COMMON_WARNING)
|
|
|
|
#define LOG_WARN(M, ...)
|
|
|
|
#else
|
|
|
|
#define LOG_WARN(M, ...) \
|
|
|
|
fprintf(stderr, "[WARN] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
|
|
|
|
#endif
|
2016-09-13 18:54:26 -07:00
|
|
|
|
2016-11-15 20:33:29 -08:00
|
|
|
#if (RAY_COMMON_LOG_LEVEL > RAY_COMMON_ERROR)
|
|
|
|
#define LOG_ERROR(M, ...)
|
|
|
|
#else
|
|
|
|
#define LOG_ERROR(M, ...) \
|
|
|
|
fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, \
|
|
|
|
errno == 0 ? "None" : strerror(errno), ##__VA_ARGS__)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (RAY_COMMON_LOG_LEVEL > RAY_COMMON_FATAL)
|
|
|
|
#define LOG_FATAL(M, ...)
|
2016-11-22 17:04:24 -08:00
|
|
|
#elif defined(_EXECINFO_H) || !defined(_WIN32)
|
2016-11-29 22:40:01 -08:00
|
|
|
#define LOG_FATAL(M, ...) \
|
|
|
|
do { \
|
|
|
|
fprintf(stderr, "[FATAL] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, \
|
|
|
|
errno == 0 ? "None" : strerror(errno), ##__VA_ARGS__); \
|
|
|
|
void *buffer[255]; \
|
|
|
|
const int calls = backtrace(buffer, sizeof(buffer) / sizeof(void *)); \
|
|
|
|
backtrace_symbols_fd(buffer, calls, 1); \
|
2016-12-02 15:10:37 -08:00
|
|
|
abort(); \
|
2016-11-23 00:54:07 -08:00
|
|
|
} while (0)
|
2016-11-22 17:04:24 -08:00
|
|
|
#else
|
2016-11-29 22:40:01 -08:00
|
|
|
#define LOG_FATAL(M, ...) \
|
|
|
|
do { \
|
|
|
|
fprintf(stderr, "[FATAL] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, \
|
|
|
|
errno == 0 ? "None" : strerror(errno), ##__VA_ARGS__); \
|
|
|
|
exit(-1); \
|
2016-11-23 00:54:07 -08:00
|
|
|
} while (0)
|
2016-11-15 20:33:29 -08:00
|
|
|
#endif
|
2016-09-17 00:03:10 -07:00
|
|
|
|
2016-12-08 20:57:08 -08:00
|
|
|
/** Assertion definitions, with optional logging. */
|
2016-11-15 20:33:29 -08:00
|
|
|
#define CHECKM(COND, M, ...) \
|
|
|
|
if (!(COND)) { \
|
|
|
|
LOG_FATAL("Check failure: %s \n" M, #COND, ##__VA_ARGS__); \
|
2016-11-10 18:13:26 -08:00
|
|
|
}
|
|
|
|
|
2016-10-29 19:25:43 -07:00
|
|
|
#define CHECK(COND) CHECKM(COND, "")
|
2016-10-03 17:55:57 -07:00
|
|
|
|
2016-11-08 14:46:34 -08:00
|
|
|
/* This should be defined if we want to check calls to DCHECK. */
|
|
|
|
#define RAY_DCHECK
|
|
|
|
|
2017-02-20 22:42:03 -08:00
|
|
|
/* Arrow also defines the DCHECK macro, so undo that definition. */
|
|
|
|
#ifdef DCHECK
|
|
|
|
#undef DCHECK
|
|
|
|
#endif
|
|
|
|
|
2016-11-08 14:46:34 -08:00
|
|
|
#ifdef RAY_DCHECK
|
|
|
|
#define DCHECK(COND) CHECK(COND)
|
|
|
|
#else
|
2016-11-04 00:41:20 -07:00
|
|
|
#define DCHECK(COND)
|
|
|
|
#endif
|
|
|
|
|
2016-10-31 15:00:15 -07:00
|
|
|
/* These are exit codes for common errors that can occur in Ray components. */
|
|
|
|
#define EXIT_COULD_NOT_BIND_PORT -2
|
|
|
|
|
2016-10-29 15:22:33 -07:00
|
|
|
/** This macro indicates that this pointer owns the data it is pointing to
|
|
|
|
* and is responsible for freeing it. */
|
|
|
|
#define OWNER
|
|
|
|
|
2016-12-08 20:57:08 -08:00
|
|
|
/** Definitions for unique ID types. */
|
2016-09-13 18:54:26 -07:00
|
|
|
#define UNIQUE_ID_SIZE 20
|
|
|
|
|
2016-11-10 18:13:26 -08:00
|
|
|
#define UNIQUE_ID_EQ(id1, id2) (memcmp((id1).id, (id2).id, UNIQUE_ID_SIZE) == 0)
|
|
|
|
|
|
|
|
#define IS_NIL_ID(id) UNIQUE_ID_EQ(id, NIL_ID)
|
|
|
|
|
2017-02-26 00:32:43 -08:00
|
|
|
typedef struct { unsigned char id[UNIQUE_ID_SIZE]; } UniqueID;
|
2016-09-13 18:54:26 -07:00
|
|
|
|
2016-11-06 17:31:14 -08:00
|
|
|
extern const UT_icd object_id_icd;
|
|
|
|
|
2017-02-26 00:32:43 -08:00
|
|
|
extern const UniqueID NIL_ID;
|
2016-09-29 21:12:06 -07:00
|
|
|
|
2016-09-18 18:06:42 -07:00
|
|
|
/* Generate a globally unique ID. */
|
2017-02-26 00:32:43 -08:00
|
|
|
UniqueID globally_unique_id(void);
|
2016-09-18 18:06:42 -07:00
|
|
|
|
2016-11-10 18:13:26 -08:00
|
|
|
#define NIL_OBJECT_ID NIL_ID
|
|
|
|
|
2017-02-26 00:32:43 -08:00
|
|
|
typedef UniqueID ObjectID;
|
2016-09-20 17:02:56 -07:00
|
|
|
|
2017-04-07 12:32:12 -07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
|
|
struct UniqueIDHasher {
|
|
|
|
/* ObjectID hashing function. */
|
|
|
|
size_t operator()(const UniqueID &id) const {
|
|
|
|
size_t result;
|
2017-04-18 15:31:49 -07:00
|
|
|
memcpy(&result, id.id, sizeof(size_t));
|
2017-04-07 12:32:12 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool operator==(const ObjectID &x, const ObjectID &y);
|
|
|
|
#endif
|
|
|
|
|
2016-12-22 03:11:46 -08:00
|
|
|
#define ID_STRING_SIZE (2 * UNIQUE_ID_SIZE + 1)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert an object ID to a hexdecimal string. This function assumes that
|
|
|
|
* buffer points to an already allocated char array of size ID_STRING_SIZE. And
|
|
|
|
* it writes a null-terminated hex-formatted string to id_string.
|
|
|
|
*
|
|
|
|
* @param obj_id The object ID to convert to a string.
|
|
|
|
* @param id_string A buffer to write the string to. It is assumed that this is
|
|
|
|
* managed by the caller and is sufficiently long to store the object ID
|
|
|
|
* string.
|
|
|
|
* @param id_length The length of the id_string buffer.
|
|
|
|
*/
|
2017-02-26 00:32:43 -08:00
|
|
|
char *ObjectID_to_string(ObjectID obj_id, char *id_string, int id_length);
|
2016-12-22 03:11:46 -08:00
|
|
|
|
2016-11-08 14:46:34 -08:00
|
|
|
/**
|
|
|
|
* Compare two object IDs.
|
|
|
|
*
|
|
|
|
* @param first_id The first object ID to compare.
|
|
|
|
* @param second_id The first object ID to compare.
|
|
|
|
* @return True if the object IDs are the same and false otherwise.
|
|
|
|
*/
|
2017-02-26 00:32:43 -08:00
|
|
|
bool ObjectID_equal(ObjectID first_id, ObjectID second_id);
|
2016-11-08 14:46:34 -08:00
|
|
|
|
2016-11-10 18:13:26 -08:00
|
|
|
/**
|
|
|
|
* Compare a object ID to the nil ID.
|
|
|
|
*
|
|
|
|
* @param id The object ID to compare to nil.
|
|
|
|
* @return True if the object ID is equal to nil.
|
|
|
|
*/
|
2017-02-26 00:32:43 -08:00
|
|
|
bool ObjectID_is_nil(ObjectID id);
|
2016-11-10 18:13:26 -08:00
|
|
|
|
2017-02-26 00:32:43 -08:00
|
|
|
typedef UniqueID DBClientID;
|
2016-11-18 19:57:51 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare two db client IDs.
|
|
|
|
*
|
|
|
|
* @param first_id The first db client ID to compare.
|
|
|
|
* @param second_id The first db client ID to compare.
|
|
|
|
* @return True if the db client IDs are the same and false otherwise.
|
|
|
|
*/
|
2017-02-26 00:32:43 -08:00
|
|
|
bool DBClientID_equal(DBClientID first_id, DBClientID second_id);
|
2016-11-18 19:57:51 -08:00
|
|
|
|
2016-12-01 02:15:21 -08:00
|
|
|
#define MAX(x, y) ((x) >= (y) ? (x) : (y))
|
|
|
|
#define MIN(x, y) ((x) <= (y) ? (x) : (y))
|
|
|
|
|
2016-12-08 20:57:08 -08:00
|
|
|
/** Definitions for computing hash digests. */
|
|
|
|
#define DIGEST_SIZE SHA256_BLOCK_SIZE
|
|
|
|
|
|
|
|
extern const unsigned char NIL_DIGEST[DIGEST_SIZE];
|
|
|
|
|
2016-09-13 18:54:26 -07:00
|
|
|
#endif
|