Skip to content

Commit 9843dd2

Browse files
committed
wl#9819 Version 2. Patch #3: ProcessInfo, OwnProcessInfo, and SocketServer
1 parent 63aaebd commit 9843dd2

File tree

5 files changed

+95
-58
lines changed

5 files changed

+95
-58
lines changed

storage/ndb/include/kernel/OwnProcessInfo.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
33
44
This program is free software; you can redistribute it and/or modify
55
it under the terms of the GNU General Public License as published by
@@ -20,9 +20,8 @@
2020

2121
class ProcessInfo;
2222

23-
void setOwnProcessInfoName(const char * pathname);
2423
void setOwnProcessInfoAngelPid(Uint32);
25-
void setOwnProcessInfoServerAddress(const struct sockaddr_in *);
24+
void setOwnProcessInfoServerAddress(struct in_addr *);
2625
void setOwnProcessInfoPort(Uint16);
2726

2827
ProcessInfo * getOwnProcessInfo(Uint16 nodeId);

storage/ndb/include/kernel/ProcessInfo.hpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
Copyright (c) 2016, 2017 Oracle and/or its affiliates. All rights reserved.
33
44
This program is free software; you can redistribute it and/or modify
55
it under the terms of the GNU General Public License as published by
@@ -34,11 +34,14 @@ class ProcessInfo {
3434
static ProcessInfo * forNodeId(Uint16);
3535
static void release(ProcessInfo *);
3636

37+
static bool isValidUri(const char *scheme, const char *path);
38+
3739
bool isValid() const;
3840
void invalidate();
3941

40-
void setConnectionName(const char *);
41-
void setConnectionName(Uint32 *signal_data);
42+
void setUriPath(const char *);
43+
void setUriPath(Uint32 *signal_data);
44+
void setUriScheme(const char *);
4245
void setProcessName(const char *);
4346
void setHostAddress(const struct sockaddr *, socklen_t);
4447
void setHostAddress(const struct in_addr *);
@@ -48,38 +51,43 @@ class ProcessInfo {
4851
void setAngelPid(Uint32 pid);
4952
void setPort(Uint16);
5053
void setNodeId(Uint16);
51-
const char * getConnectionName() const { return connection_name; };
54+
55+
int getServiceUri(char * buffer, size_t length) const;
56+
57+
const char * getUriPath() const { return uri_path; };
58+
const char * getUriScheme() const { return uri_scheme; };
5259
const char * getProcessName() const { return process_name; };
5360
const char * getHostAddress() const { return host_address; };
5461
int getPid() const;
5562
int getAngelPid() const { return angel_process_id; };
5663
int getPort() const { return application_port; };
5764
int getNodeId() const { return node_id; };
5865

59-
60-
/* Interface for Qmgr to build ProcessInfo for remote node
61-
from received signal */
62-
void initializeFromProcessInfoRep(ProcessInfoRep *);
63-
64-
STATIC_CONST( ConnectionNameLength = 128 );
65-
STATIC_CONST( ConnectionNameLengthInWords = 32);
66+
STATIC_CONST( UriPathLength = 128 );
67+
STATIC_CONST( UriPathLengthInWords = 32 );
68+
STATIC_CONST( UriSchemeLength = 16);
6669
STATIC_CONST( ProcessNameLength = 48 );
6770
STATIC_CONST( AddressStringLength = 48 ); // Long enough for IPv6
6871
STATIC_CONST( AddressStringLengthInWords = 12);
6972

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

76+
/* Interface for Qmgr to build ProcessInfo for remote node
77+
from received signal */
78+
void initializeFromProcessInfoRep(ProcessInfoRep *);
79+
7380

7481
private: /* Data Members */
75-
char connection_name[ConnectionNameLength];
82+
char uri_path[UriPathLength];
7683
char host_address[AddressStringLength];
7784
char process_name[ProcessNameLength];
85+
char uri_scheme[UriSchemeLength];
7886
Uint32 node_id;
7987
Uint32 process_id;
8088
Uint32 angel_process_id;
8189
Uint32 application_port;
82-
}; // 240 bytes per node
90+
}; // 256 bytes per node
8391

8492

8593
inline bool ProcessInfo::isValid() const {

storage/ndb/src/common/util/OwnProcessInfo.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
Copyright (c) 2016, 2017 Oracle and/or its affiliates. All rights reserved.
33
44
This program is free software; you can redistribute it and/or modify
55
it under the terms of the GNU General Public License as published by
@@ -18,10 +18,8 @@
1818
#include "ndb_global.h"
1919
#include "ProcessInfo.hpp"
2020
#include "OwnProcessInfo.hpp"
21-
#include <EventLogger.hpp>
2221
#include <NdbMutex.h>
2322

24-
extern EventLogger * g_eventLogger;
2523
const char * ndb_basename(const char *path);
2624

2725
extern "C" {
@@ -35,24 +33,17 @@ NdbLockable theApiMutex;
3533
/* Public API
3634
*
3735
*/
38-
void setOwnProcessInfoName(const char *pathname)
39-
{
40-
theApiMutex.lock();
41-
singletonInfo.setProcessName(ndb_basename(pathname));
42-
theApiMutex.unlock();
43-
}
44-
4536
void setOwnProcessInfoAngelPid(Uint32 pid)
4637
{
4738
theApiMutex.lock();
4839
singletonInfo.setAngelPid(pid);
4940
theApiMutex.unlock();
5041
}
5142

52-
void setOwnProcessInfoServerAddress(const struct sockaddr_in * addr)
43+
void setOwnProcessInfoServerAddress(struct in_addr * addr)
5344
{
5445
theApiMutex.lock();
55-
singletonInfo.setHostAddress((const struct sockaddr *) addr, sizeof(addr));
46+
singletonInfo.setHostAddress(addr);
5647
theApiMutex.unlock();
5748
}
5849

@@ -124,10 +115,6 @@ ProcessInfo * getOwnProcessInfo(Uint16 nodeId) {
124115
else
125116
getNameFromEnvironment();
126117
}
127-
128-
g_eventLogger->info("getOwnProcessInfo: pid %d, name %s, addr %s, node %d",
129-
singletonInfo.getPid(), singletonInfo.getProcessName(),
130-
singletonInfo.getHostAddress(), singletonInfo.getNodeId());
131118
}
132119

133120
return & singletonInfo;

storage/ndb/src/common/util/ProcessInfo.cpp

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
33
44
This program is free software; you can redistribute it and/or modify
55
it under the terms of the GNU General Public License as published by
@@ -21,15 +21,27 @@
2121
#include "ProcessInfo.hpp"
2222
#include "OwnProcessInfo.hpp"
2323
#include "signaldata/ProcessInfoRep.hpp"
24-
#include "EventLogger.hpp"
24+
#include "BaseString.hpp"
2525
#include "ndb_net.h"
2626
#include "ndb_socket.h"
2727
#include "NdbTCP.h"
2828

29-
extern EventLogger * g_eventLogger;
30-
3129
/* Utility Functions */
3230

31+
static inline bool isValidUriSchemeChar(char c) {
32+
return (c >= 'a' && c <= 'z') ||
33+
(c >= '0' && c <= '9') ||
34+
(c == '+') || (c == '.') || (c == '-');
35+
}
36+
37+
static bool valid_URI_scheme(const char * s) {
38+
while(*s) {
39+
if(! isValidUriSchemeChar(*s)) return false;
40+
s++;
41+
}
42+
return true;
43+
}
44+
3345
static inline bool isUtf8CharMultibyte(char c) { // is any part of multi-byte char
3446
return (c & 0x80);
3547
}
@@ -66,9 +78,10 @@ ProcessInfo::ProcessInfo()
6678

6779
void ProcessInfo::invalidate()
6880
{
69-
memset(connection_name, 0, ConnectionNameLength);
81+
memset(uri_path, 0, UriPathLength);
7082
memset(host_address, 0, AddressStringLength);
7183
memset(process_name, 0, ProcessNameLength);
84+
strcpy(uri_scheme, "ndb");
7285
node_id = 0;
7386
process_id = 0;
7487
angel_process_id = 0;
@@ -88,12 +101,11 @@ ProcessInfo * ProcessInfo::forNodeId(Uint16 nodeId)
88101
return process;
89102
/* Make a copy */
90103
ProcessInfo * self = new ProcessInfo();
91-
strncpy(self->host_address, process->host_address, AddressStringLength);
104+
self->node_id = nodeId; // do not copy node id
92105
strncpy(self->process_name, process->process_name, ProcessNameLength);
93-
self->node_id = nodeId; // do not copy node id or connection name
94106
self->process_id = process->process_id;
95107
self->angel_process_id = process->angel_process_id;
96-
self->application_port = process->application_port;
108+
/* Do not copy any of the fields that will be set from set_service_uri() */
97109
return self;
98110
}
99111

@@ -105,6 +117,16 @@ void ProcessInfo::release(ProcessInfo *self)
105117
delete self;
106118
}
107119

120+
/* Check URI components for syntactic validity
121+
*/
122+
bool ProcessInfo::isValidUri(const char *scheme, const char *path)
123+
{
124+
if(path && path[0] == '/' && path[1] == '/')
125+
return false;
126+
return valid_URI_scheme(scheme);
127+
}
128+
129+
108130
void ProcessInfo::setProcessName(const char * name) {
109131
size_t len = truncateUtf8(name, ProcessNameLength);
110132
strncpy(process_name, name, len);
@@ -120,20 +142,27 @@ int ProcessInfo::getPid() const {
120142
return process_id;
121143
}
122144

123-
void ProcessInfo::setConnectionName(const char * name) {
124-
size_t len = truncateUtf8(name, ConnectionNameLength);
125-
strncpy(connection_name, name, len);
126-
connection_name[len] = 0;
127-
g_eventLogger->info("ProcessInfo set connection name: %s", connection_name);
145+
void ProcessInfo::setUriPath(const char * path) {
146+
size_t len = truncateUtf8(path, UriPathLength);
147+
strncpy(uri_path, path, len);
148+
uri_path[len] = 0;
128149
}
129150

130-
void ProcessInfo::setConnectionName(Uint32 * signal_data) {
131-
memcpy(connection_name, signal_data, ConnectionNameLength);
151+
void ProcessInfo::setUriPath(Uint32 * signal_data) {
152+
memcpy(uri_path, signal_data, UriPathLength);
153+
}
154+
155+
void ProcessInfo::setUriScheme(const char * scheme) {
156+
if(scheme && scheme[0] && valid_URI_scheme(scheme)) {
157+
strncpy(uri_scheme, scheme, UriSchemeLength);
158+
uri_scheme[UriSchemeLength - 1] = '\0';
159+
}
132160
}
133161

134162
void ProcessInfo::setHostAddress(const char * address_string) {
135163
if(address_string) {
136164
strncpy(host_address, address_string, AddressStringLength);
165+
host_address[AddressStringLength-1] = '\0';
137166
}
138167
}
139168

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

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

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

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

161191
void ProcessInfo::setNodeId(Uint16 nodeId) {
162192
node_id = nodeId;
163193
}
164194

165195
void ProcessInfo::initializeFromProcessInfoRep(ProcessInfoRep * signal) {
166-
g_eventLogger->info("Received ProcessInfoRep. "
167-
"Node: %d, Port: %d, Name: %s, Pid: %d",
168-
signal->node_id, signal->application_port,
169-
signal->process_name, signal->process_id);
170196
if(isValid()) invalidate();
171197
setProcessName( (char *) signal->process_name);
198+
setUriScheme( (char *) signal->uri_scheme);
172199
process_id = signal->process_id;
173200
angel_process_id = signal->angel_process_id;
174201
application_port = signal->application_port;
@@ -177,13 +204,29 @@ void ProcessInfo::initializeFromProcessInfoRep(ProcessInfoRep * signal) {
177204

178205
void ProcessInfo::buildProcessInfoReport(ProcessInfoRep *signal) {
179206
memcpy(signal->process_name, process_name, ProcessNameLength);
207+
memcpy(signal->uri_scheme, uri_scheme, UriSchemeLength);
180208
signal->node_id = node_id;
181209
signal->process_id = process_id;
182210
signal->angel_process_id = angel_process_id;
183211
signal->application_port = application_port;
212+
}
213+
214+
int ProcessInfo::getServiceUri(char * buffer, size_t buf_len) const {
215+
int len;
184216

185-
g_eventLogger->info("Created ProcessInfoRep. "
186-
"Node: %d, Port: %d, Name: %s, Pid: %d",
187-
signal->node_id, signal->application_port,
188-
signal->process_name, signal->process_id);
217+
/* Path must begin with a single slash if authority was present. */
218+
const char * path_prefix = "";
219+
if(uri_path[0] != '\0' && uri_path[0] != '/') {
220+
path_prefix = "/";
221+
}
222+
223+
if(application_port > 0) {
224+
len = BaseString::snprintf(buffer, buf_len, "%s://%s:%d%s%s", uri_scheme,
225+
host_address, application_port, path_prefix, uri_path);
226+
} else {
227+
len = BaseString::snprintf(buffer, buf_len, "%s://%s%s%s", uri_scheme,
228+
host_address, path_prefix, uri_path);
229+
}
230+
return len;
189231
}
232+

storage/ndb/src/common/util/SocketServer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
2+
Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
33
44
This program is free software; you can redistribute it and/or modify
55
it under the terms of the GNU General Public License as published by
@@ -142,7 +142,7 @@ SocketServer::setup(SocketServer::Service * service,
142142
DBUG_RETURN(false);
143143
}
144144
*port = ntohs(serv_addr.sin_port);
145-
setOwnProcessInfoServerAddress(&serv_addr);
145+
setOwnProcessInfoServerAddress(& serv_addr.sin_addr);
146146

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

0 commit comments

Comments
 (0)