Skip to content

Commit

Permalink
wl#9819 Version 2. Patch #3: ProcessInfo, OwnProcessInfo, and SocketS…
Browse files Browse the repository at this point in the history
…erver
  • Loading branch information
jdduncan committed Mar 30, 2017
1 parent 63aaebd commit 9843dd2
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 58 deletions.
5 changes: 2 additions & 3 deletions storage/ndb/include/kernel/OwnProcessInfo.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -20,9 +20,8 @@

class ProcessInfo;

void setOwnProcessInfoName(const char * pathname);
void setOwnProcessInfoAngelPid(Uint32);
void setOwnProcessInfoServerAddress(const struct sockaddr_in *);
void setOwnProcessInfoServerAddress(struct in_addr *);
void setOwnProcessInfoPort(Uint16);

ProcessInfo * getOwnProcessInfo(Uint16 nodeId);
Expand Down
34 changes: 21 additions & 13 deletions storage/ndb/include/kernel/ProcessInfo.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2016, 2017 Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -34,11 +34,14 @@ class ProcessInfo {
static ProcessInfo * forNodeId(Uint16);
static void release(ProcessInfo *);

static bool isValidUri(const char *scheme, const char *path);

bool isValid() const;
void invalidate();

void setConnectionName(const char *);
void setConnectionName(Uint32 *signal_data);
void setUriPath(const char *);
void setUriPath(Uint32 *signal_data);
void setUriScheme(const char *);
void setProcessName(const char *);
void setHostAddress(const struct sockaddr *, socklen_t);
void setHostAddress(const struct in_addr *);
Expand All @@ -48,38 +51,43 @@ class ProcessInfo {
void setAngelPid(Uint32 pid);
void setPort(Uint16);
void setNodeId(Uint16);
const char * getConnectionName() const { return connection_name; };

int getServiceUri(char * buffer, size_t length) const;

const char * getUriPath() const { return uri_path; };
const char * getUriScheme() const { return uri_scheme; };
const char * getProcessName() const { return process_name; };
const char * getHostAddress() const { return host_address; };
int getPid() const;
int getAngelPid() const { return angel_process_id; };
int getPort() const { return application_port; };
int getNodeId() const { return node_id; };


/* Interface for Qmgr to build ProcessInfo for remote node
from received signal */
void initializeFromProcessInfoRep(ProcessInfoRep *);

STATIC_CONST( ConnectionNameLength = 128 );
STATIC_CONST( ConnectionNameLengthInWords = 32);
STATIC_CONST( UriPathLength = 128 );
STATIC_CONST( UriPathLengthInWords = 32 );
STATIC_CONST( UriSchemeLength = 16);
STATIC_CONST( ProcessNameLength = 48 );
STATIC_CONST( AddressStringLength = 48 ); // Long enough for IPv6
STATIC_CONST( AddressStringLengthInWords = 12);

/* Interface for ClusterManager to create signal */
void buildProcessInfoReport(ProcessInfoRep *);

/* Interface for Qmgr to build ProcessInfo for remote node
from received signal */
void initializeFromProcessInfoRep(ProcessInfoRep *);


private: /* Data Members */
char connection_name[ConnectionNameLength];
char uri_path[UriPathLength];
char host_address[AddressStringLength];
char process_name[ProcessNameLength];
char uri_scheme[UriSchemeLength];
Uint32 node_id;
Uint32 process_id;
Uint32 angel_process_id;
Uint32 application_port;
}; // 240 bytes per node
}; // 256 bytes per node


inline bool ProcessInfo::isValid() const {
Expand Down
19 changes: 3 additions & 16 deletions storage/ndb/src/common/util/OwnProcessInfo.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2016, 2017 Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -18,10 +18,8 @@
#include "ndb_global.h"
#include "ProcessInfo.hpp"
#include "OwnProcessInfo.hpp"
#include <EventLogger.hpp>
#include <NdbMutex.h>

extern EventLogger * g_eventLogger;
const char * ndb_basename(const char *path);

extern "C" {
Expand All @@ -35,24 +33,17 @@ NdbLockable theApiMutex;
/* Public API
*
*/
void setOwnProcessInfoName(const char *pathname)
{
theApiMutex.lock();
singletonInfo.setProcessName(ndb_basename(pathname));
theApiMutex.unlock();
}

void setOwnProcessInfoAngelPid(Uint32 pid)
{
theApiMutex.lock();
singletonInfo.setAngelPid(pid);
theApiMutex.unlock();
}

void setOwnProcessInfoServerAddress(const struct sockaddr_in * addr)
void setOwnProcessInfoServerAddress(struct in_addr * addr)
{
theApiMutex.lock();
singletonInfo.setHostAddress((const struct sockaddr *) addr, sizeof(addr));
singletonInfo.setHostAddress(addr);
theApiMutex.unlock();
}

Expand Down Expand Up @@ -124,10 +115,6 @@ ProcessInfo * getOwnProcessInfo(Uint16 nodeId) {
else
getNameFromEnvironment();
}

g_eventLogger->info("getOwnProcessInfo: pid %d, name %s, addr %s, node %d",
singletonInfo.getPid(), singletonInfo.getProcessName(),
singletonInfo.getHostAddress(), singletonInfo.getNodeId());
}

return & singletonInfo;
Expand Down
91 changes: 67 additions & 24 deletions storage/ndb/src/common/util/ProcessInfo.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -21,15 +21,27 @@
#include "ProcessInfo.hpp"
#include "OwnProcessInfo.hpp"
#include "signaldata/ProcessInfoRep.hpp"
#include "EventLogger.hpp"
#include "BaseString.hpp"
#include "ndb_net.h"
#include "ndb_socket.h"
#include "NdbTCP.h"

extern EventLogger * g_eventLogger;

/* Utility Functions */

static inline bool isValidUriSchemeChar(char c) {
return (c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') ||
(c == '+') || (c == '.') || (c == '-');
}

static bool valid_URI_scheme(const char * s) {
while(*s) {
if(! isValidUriSchemeChar(*s)) return false;
s++;
}
return true;
}

static inline bool isUtf8CharMultibyte(char c) { // is any part of multi-byte char
return (c & 0x80);
}
Expand Down Expand Up @@ -66,9 +78,10 @@ ProcessInfo::ProcessInfo()

void ProcessInfo::invalidate()
{
memset(connection_name, 0, ConnectionNameLength);
memset(uri_path, 0, UriPathLength);
memset(host_address, 0, AddressStringLength);
memset(process_name, 0, ProcessNameLength);
strcpy(uri_scheme, "ndb");
node_id = 0;
process_id = 0;
angel_process_id = 0;
Expand All @@ -88,12 +101,11 @@ ProcessInfo * ProcessInfo::forNodeId(Uint16 nodeId)
return process;
/* Make a copy */
ProcessInfo * self = new ProcessInfo();
strncpy(self->host_address, process->host_address, AddressStringLength);
self->node_id = nodeId; // do not copy node id
strncpy(self->process_name, process->process_name, ProcessNameLength);
self->node_id = nodeId; // do not copy node id or connection name
self->process_id = process->process_id;
self->angel_process_id = process->angel_process_id;
self->application_port = process->application_port;
/* Do not copy any of the fields that will be set from set_service_uri() */
return self;
}

Expand All @@ -105,6 +117,16 @@ void ProcessInfo::release(ProcessInfo *self)
delete self;
}

/* Check URI components for syntactic validity
*/
bool ProcessInfo::isValidUri(const char *scheme, const char *path)
{
if(path && path[0] == '/' && path[1] == '/')
return false;
return valid_URI_scheme(scheme);
}


void ProcessInfo::setProcessName(const char * name) {
size_t len = truncateUtf8(name, ProcessNameLength);
strncpy(process_name, name, len);
Expand All @@ -120,20 +142,27 @@ int ProcessInfo::getPid() const {
return process_id;
}

void ProcessInfo::setConnectionName(const char * name) {
size_t len = truncateUtf8(name, ConnectionNameLength);
strncpy(connection_name, name, len);
connection_name[len] = 0;
g_eventLogger->info("ProcessInfo set connection name: %s", connection_name);
void ProcessInfo::setUriPath(const char * path) {
size_t len = truncateUtf8(path, UriPathLength);
strncpy(uri_path, path, len);
uri_path[len] = 0;
}

void ProcessInfo::setConnectionName(Uint32 * signal_data) {
memcpy(connection_name, signal_data, ConnectionNameLength);
void ProcessInfo::setUriPath(Uint32 * signal_data) {
memcpy(uri_path, signal_data, UriPathLength);
}

void ProcessInfo::setUriScheme(const char * scheme) {
if(scheme && scheme[0] && valid_URI_scheme(scheme)) {
strncpy(uri_scheme, scheme, UriSchemeLength);
uri_scheme[UriSchemeLength - 1] = '\0';
}
}

void ProcessInfo::setHostAddress(const char * address_string) {
if(address_string) {
strncpy(host_address, address_string, AddressStringLength);
host_address[AddressStringLength-1] = '\0';
}
}

Expand All @@ -146,6 +175,8 @@ void ProcessInfo::setHostAddress(const struct sockaddr * addr, socklen_t len) {
}

void ProcessInfo::setHostAddress(const struct in_addr * addr) {
/* If address passed in is a wildcard address, do not use it. */
if(addr->s_addr != htonl(INADDR_ANY))
Ndb_inet_ntop(AF_INET, addr, host_address, AddressStringLength);
}

Expand All @@ -155,20 +186,16 @@ void ProcessInfo::setAngelPid(Uint32 pid) {

void ProcessInfo::setPort(Uint16 port) {
application_port = port;
g_eventLogger->info("ProcessInfo set port: %d", application_port);
}

void ProcessInfo::setNodeId(Uint16 nodeId) {
node_id = nodeId;
}

void ProcessInfo::initializeFromProcessInfoRep(ProcessInfoRep * signal) {
g_eventLogger->info("Received ProcessInfoRep. "
"Node: %d, Port: %d, Name: %s, Pid: %d",
signal->node_id, signal->application_port,
signal->process_name, signal->process_id);
if(isValid()) invalidate();
setProcessName( (char *) signal->process_name);
setUriScheme( (char *) signal->uri_scheme);
process_id = signal->process_id;
angel_process_id = signal->angel_process_id;
application_port = signal->application_port;
Expand All @@ -177,13 +204,29 @@ void ProcessInfo::initializeFromProcessInfoRep(ProcessInfoRep * signal) {

void ProcessInfo::buildProcessInfoReport(ProcessInfoRep *signal) {
memcpy(signal->process_name, process_name, ProcessNameLength);
memcpy(signal->uri_scheme, uri_scheme, UriSchemeLength);
signal->node_id = node_id;
signal->process_id = process_id;
signal->angel_process_id = angel_process_id;
signal->application_port = application_port;
}

int ProcessInfo::getServiceUri(char * buffer, size_t buf_len) const {
int len;

g_eventLogger->info("Created ProcessInfoRep. "
"Node: %d, Port: %d, Name: %s, Pid: %d",
signal->node_id, signal->application_port,
signal->process_name, signal->process_id);
/* Path must begin with a single slash if authority was present. */
const char * path_prefix = "";
if(uri_path[0] != '\0' && uri_path[0] != '/') {
path_prefix = "/";
}

if(application_port > 0) {
len = BaseString::snprintf(buffer, buf_len, "%s://%s:%d%s%s", uri_scheme,
host_address, application_port, path_prefix, uri_path);
} else {
len = BaseString::snprintf(buffer, buf_len, "%s://%s%s%s", uri_scheme,
host_address, path_prefix, uri_path);
}
return len;
}

4 changes: 2 additions & 2 deletions storage/ndb/src/common/util/SocketServer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -142,7 +142,7 @@ SocketServer::setup(SocketServer::Service * service,
DBUG_RETURN(false);
}
*port = ntohs(serv_addr.sin_port);
setOwnProcessInfoServerAddress(&serv_addr);
setOwnProcessInfoServerAddress(& serv_addr.sin_addr);

DBUG_PRINT("info",("bound to %u", *port));

Expand Down

0 comments on commit 9843dd2

Please sign in to comment.