/*****************************************************************************/
/*
                               Instance.h

*/
/*****************************************************************************/

#ifndef INSTANCE_H_LOADED
#define INSTANCE_H_LOADED 1

#include "wasd.h"

/* these must be sequential from 1 to whatever (used as array indices) */
#define INSTANCE_CLUSTER            0x01  /* join cluster instance */
#define INSTANCE_CLUSTER_DO         0x02  /* distributes cluster directive */
#define INSTANCE_CLUSTER_NOTIFY     0x03  /* locks notifications */
#define INSTANCE_CLUSTER_PROXYMAINT 0x04  /* locks proxy cache maintainance */
#define INSTANCE_CLUSTER_STATUS     0x05  /* distributes instance status */
#define INSTANCE_CLUSTER_LOCK_COUNT    5  /* DON'T FORGET TO ADJUST! */
#define INSTANCE_NODE               0x06  /* join node instance */
#define INSTANCE_NODE_DO            0x07  /* distributes node directive */
#define INSTANCE_NODE_WATCH         0x08  /* WATCH is in use */
#define INSTANCE_NODE_SINGLE        0x09  /* starting config of one instance */
#define INSTANCE_NODE_JOINING       0x0a  /* instance is joining the node */
#define INSTANCE_NODE_READY         0x0b  /* instance is ready for requests */
#define INSTANCE_NODE_SUPERVISOR    0x0c  /* delegates per-node supervisor */
#define INSTANCE_NODE_SOCKET        0x0d  /* lock socket allocation */
#define INSTANCE_NODE_LOCK_COUNT       8  /* DITTO! */
                                          /* AND ADJUST CODES BELOW!! */
#define INSTANCE_LOCK_CODES 0x00, \
        0x01,0x02,0x03,0x04, \
        0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d

#define INSTANCE_LOCK_USES "", \
        "cluster", "do", "notify", "proxy-maint", "status", \
        "node", "do", "watch", "single", "join", "ready", "super", "socket"

#define INSTANCE_LOCK_COUNT INSTANCE_CLUSTER_LOCK_COUNT + \
                            INSTANCE_NODE_LOCK_COUNT

/* must remain below INSTANCE_LOCK_PRINTABLE */
#define INSTANCE_NODE_SOCKIP4       0x14  /* distributes IPv4 socket BG: */
#define INSTANCE_NODE_SOCKIP6       0x16  /* distributes IPv6 socket BG: */

#define INSTANCE_LOCK_PRINTABLE     0x20  /* (space) */

#define INSTANCE_MUTEX_HTTPD           1  /* main global section */
#define INSTANCE_MUTEX_ACTIVITY        2  /* actvitity stats global section */
#define INSTANCE_MUTEX_AUTH_CACHE      3  /* authorization cache */
#define INSTANCE_MUTEX_AUTH_TOKEN      4  /* auth token cache */
#define INSTANCE_MUTEX_PROXY_VERIFY    5  /* proxy verify records */
#define INSTANCE_MUTEX_SSL_CACHE       6  /* SSL session cache */
#define INSTANCE_MUTEX_COUNT           6  /* DON'T FORGET TO ADJUST! */
#define INSTANCE_MUTEX_DESCR \
   { NULL, "MUTEX_HTTPD", "MUTEX_ACTIVITY", \
     "MUTEX_AUTH_CACHE", "MUTEX_AUTH_TOKEN", \
     "MUTEX_PROXY_VERIFY", "MUTEX_SSL_CACHE" }  /* ditto!! */

/* maximum number of (WASD) processes per node */
#define INSTANCE_MAX 8

/* maximum number of (WASD) nodes per cluster */
#define INSTANCE_CLUSTER_MAX 8

/* maximum number of sockets that can be shared between per-node instances */
#define INSTANCE_LOCK_SOCKET_MAX 64

/* maximum number of locks that can be $GETLKI()ed at the one time */
#define INSTANCE_REPORT_LOCK_MAX 256

/* minimum seconds between node supervisor polls */
#define INSTANCE_SUPERVISOR_POLL 15

/* maximum seconds before we commence a rolling restart */
#define INSTANCE_RESTART_SECONDS 3600

/* number of seconds we'll wait before considering it a lost cause */
#define INSTANCE_MUTEX_WAIT 300

/* a sentinal to indicate as many instances as CPUs should be created */
#define INSTANCE_PER_CPU -999

/* default environment number is one */
#define INSTANCE_ENV_NUMBER_DEFAULT 1

/* maximum /INSTANCE=<integer> allowed (4 bits) */
#define INSTANCE_ENV_NUMBER_MAX 15

/* number of seconds to wait to deliver joining message */
#define INSTANCE_JOINING_WAIT_SECS 30

/* number of seconds to wait to deliver new SSL ticket */
#define INSTANCE_TICKET_WAIT_SECS 10

/* retry ten times (over ten minutes) to deliver ticket */
#define INSTANCE_REFRESH_TICKET_RETRY 10

/* maximum number of status entries per cluster */
#define INSTANCE_STATUS_TABLE_MAX (INSTANCE_MAX * INSTANCE_CLUSTER_MAX)

/* e.g. "KLAATU::WASD1:65000\0" */
#define INSTANCE_STATUS_NAME_SIZE (8+12)

/* e.g. "11.2.0a\0" */
#define INSTANCE_STATUS_VERSION_SIZE 8

/* number of seconds to wait for instance status lock */
#define INSTANCE_STATUS_UPDATE_WAIT_SECS 1

/* retry an update every second for thirty seconds */
#define INSTANCE_STATUS_UPDATE_RETRIES 20

/* minutes before an entry is considered stale */
#define INSTANCE_STATUS_STALE_MINS 3

/*******************/
/* data structures */
/*******************/

#ifndef __VAX
#   pragma member_alignment __save
#   pragma member_alignment
#endif

typedef struct InstanceLockStruct INSTANCE_LOCK;

struct InstanceLockStruct
{
   BOOL  InUse;
   int  NameLength;
   char  Name [32];
   struct dsc$descriptor_s  NameDsc;
   struct lksb  Lksb;
   CALL_BACK  AstFunction;
};

typedef struct InstanceSocketLockStruct INSTANCE_SOCKET_LOCK;

struct InstanceSocketLockStruct
{
   char  Name [32];
   struct lksb  Lksb;
};

typedef struct InstanceStatusStruct INSTANCE_STATUS;

/* squeeze all of this into a 64 byte lock value block */
struct InstanceStatusStruct
{
   char  InstanceName [INSTANCE_STATUS_NAME_SIZE],
         HttpdVersion [INSTANCE_STATUS_VERSION_SIZE];
   ushort  MinuteCount,           /* number of requests in the past minute */
           StartupCount;          /* something seriously amiss if > 65535 */
   ulong  ExitStatus,
          HourCount;              /* number of requests in the past hour */
   ulong  ExitTime64 [2],
          StartTime64 [2],
          UpdateTime64 [2];
};

typedef struct InstanceNodeStruct INSTANCE_NODE_DATA;

/* used to store per-instance data for a single node */
struct InstanceNodeStruct
{
    BOOL  SupervisorUpdate;
    char  InstanceName [INSTANCE_STATUS_NAME_SIZE];
    ulong  ExitStatus,
           StartupCount;
    ulong  ExitTime64 [2],
           StartTime64 [2];
    /* 60 minutes of per-minute request count */
    ushort  RequestCount [60];
};

#ifndef __VAX
#   pragma member_alignment __restore
#endif                

/***********************/
/* function prototypes */
/***********************/

void InstanceExit ();
InstanceGblSecDecrLong (long*);
InstanceGblSecIncrLong (long*);
InstanceGblSecSetLong (long*, long);
InstanceFinalInit ();
InstanceLock (int);
int InstanceLockControl ();
int InstanceLockInit ();
int InstanceLockList (int, char*, char**);
int InstanceLockNoWait (int);
InstanceLockReport (REQUEST_STRUCT*);
InstanceLockReportData (REQUEST_STRUCT*, unsigned long*);
InstanceMutexLock ();
InstanceMutexUnLock ();
InstanceNodeSupervisorAst (int);
InstanceNodeJoiningAst ();
int InstanceNotifyNow (int, void*);
int InstanceNotifySet (int, CALL_BACK);
int InstanceNotifySetAst (int);
int InstanceNotifyWait (int, void*, int);
char* InstanceParseLockName (char*);
InstanceProcessName ();
InstanceServerInit ();
int InstanceSessionTicketKey (uchar*);
int InstanceSocketAdmin (short);
int InstanceSocketForAdmin (char*, short*);
char* InstanceSocket (IPADDRESS*, short, char*);
void InstanceStatusAdminReport (REQUEST_STRUCT*);
void InstanceStatusCliReport (REQUEST_STRUCT*);
void InstanceStatusExit (int);
INSTANCE_STATUS* InstanceStatusFind (char*);
void InstanceStatusNow ();
void InstanceStatusPurge ();
void InstanceStatusReset ();
void InstanceStatusUpdate (struct lksb*);
BOOL InstanceSupervisor ();
int InstanceUnLock (int);
InstanceLockUnlockControl ();
int InstanceUseConfig ();

#endif /* INSTANCE_H_LOADED */

/*****************************************************************************/

