Skip to content
This repository was archived by the owner on Jun 10, 2022. It is now read-only.

Commit f5492bd

Browse files
committed
see #1 #2 #3 #4
1 parent c498b78 commit f5492bd

File tree

17 files changed

+457
-106
lines changed

17 files changed

+457
-106
lines changed

conf/system.ini

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ logsDir=logs/
99
; 日志分卷大小
1010
logRollSize=52428800
1111
; 1: LOG_TRACE 2: LOG_DEBUG 3: LOG_INFO
12-
logLevel=1
12+
logLevel=3
1313
isAsync=no
1414

1515
[adserver]
@@ -28,7 +28,7 @@ serverName=mc-server
2828

2929
[http]
3030
host=0.0.0.0
31-
port=20010
31+
port=10053
3232
timeout=3
3333
threadNum=4
3434
serverName=adinf-adserver
@@ -45,14 +45,11 @@ threadNum=4
4545
serverName=head-server
4646

4747
[kafkac_out]
48-
;是否是 v0.9.x 新协议获取
49-
isNewConsumerOut=yes
5048
; 支持同时消费多个 topic, 多个用逗号分隔
51-
topicNameOut=test
49+
topicNameOut=fa_status_add
5250
groupIdOut=test_group_id
53-
brokerListOut=10.13.4.161:9192,10.13.4.160:9192
51+
brokerListOut=10.77.96.136:9192,10.77.96.137:9192
5452
kafkaDebugOut=none
55-
offsetPathOut=./
5653
statIntervalOut=60000
5754

5855
[timer]

src/AdbaseConfig.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
std::string groupId##name;\
1515
std::string brokerListConsumer##name;\
1616
std::string kafkaDebug##name;\
17-
std::string offsetPath##name;\
18-
std::string statInterval##name;\
19-
bool isNewConsumer##name;
17+
std::string statInterval##name;
2018
#endif
2119
#ifndef DECLARE_KAFKA_PRODUCER_CONFIG
2220
#define DECLARE_KAFKA_PRODUCER_CONFIG(name) \
@@ -85,6 +83,7 @@ class App;
8583
namespace app {
8684
class Message;
8785
class Storage;
86+
class Metrics;
8887
}
8988
typedef struct adserverContext {
9089
AdbaseConfig* config;
@@ -93,6 +92,7 @@ typedef struct adserverContext {
9392
adbase::metrics::Metrics* metrics;
9493
// 前后端交互数据指针添加到下面
9594
app::Storage* storage;
95+
app::Metrics* appMetrics;
9696
} AdServerContext;
9797

9898
typedef struct aimsContext {
@@ -101,13 +101,15 @@ typedef struct aimsContext {
101101
// 消息队列交互上下文
102102
app::Message* message;
103103
app::Storage* storage;
104+
app::Metrics* appMetrics;
104105
} AimsContext;
105106
typedef struct timerContext {
106107
AdbaseConfig* config;
107108
adbase::EventBasePtr mainEventBase;
108109
App* app;
109110
// 定时器交互上下文
110111
app::Storage* storage;
112+
app::Metrics* appMetrics;
111113
} TimerContext;
112114

113115
#endif

src/Aims.cpp

Lines changed: 25 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,5 @@
11
#include "Aims.hpp"
22

3-
// {{{ macros
4-
5-
#define STOP_KAFKA_CONSUMER(name) do {\
6-
} while(0)
7-
8-
#define INIT_KAFKA_PRODUCER(name) do {\
9-
_kafkaProducerCallback##name = new aims::kafka::Producer##name(_context);\
10-
_kafkaProducer##name = new adbase::kafka::Producer(_configure->brokerListProducer##name,\
11-
_configure->queueLength##name, _configure->debug##name);\
12-
_kafkaProducer##name->setSendHandler(std::bind(&aims::kafka::Producer##name::send,\
13-
_kafkaProducerCallback##name,\
14-
std::placeholders::_1, std::placeholders::_2,\
15-
std::placeholders::_3, std::placeholders::_4));\
16-
_kafkaProducer##name->setAckHandler(std::bind(&aims::kafka::Producer##name::ackCallback, \
17-
_kafkaProducerCallback##name, std::placeholders::_1));\
18-
_kafkaProducer##name->setErrorHandler(std::bind(&aims::kafka::Producer##name::errorCallback, \
19-
_kafkaProducerCallback##name, std::placeholders::_1));\
20-
} while(0)
21-
#define START_KAFKA_PRODUCER(name) do {\
22-
_kafkaProducer##name->start();\
23-
} while(0)
24-
#define STOP_KAFKA_PRODUCER(name) do {\
25-
if (_kafkaProducer##name != nullptr) {\
26-
_kafkaProducer##name->stop();\
27-
}\
28-
if (_kafkaProducerCallback##name != nullptr) {\
29-
delete _kafkaProducerCallback##name;\
30-
_kafkaProducerCallback##name = nullptr;\
31-
}\
32-
} while(0)
33-
34-
// }}}
353
// {{{ Aims::Aims()
364

375
Aims::Aims(AimsContext* context) :
@@ -52,10 +20,15 @@ Aims::~Aims() {
5220
void Aims::run() {
5321
// 初始化 server
5422
init();
23+
_kafka->start();
24+
}
5525

56-
for(auto &t : _kafkas) {
57-
t.second->start();
58-
}
26+
// }}}
27+
// {{{ void Aims::reload()
28+
29+
void Aims::reload() {
30+
std::vector<std::string> topicNames = adbase::explode(_configure->topicNameConsumerOut, ',', true);
31+
_kafka->setTopics(topicNames);
5932
}
6033

6134
// }}}
@@ -69,11 +42,10 @@ void Aims::init() {
6942
// {{{ void Aims::stop()
7043

7144
void Aims::stop() {
72-
for (auto &t : _kafkas) {
73-
if (t.second != nullptr) {
74-
t.second->stop();
75-
}
76-
}
45+
if (_kafka != nullptr) {
46+
_kafka->stop();
47+
delete _kafka;
48+
}
7749
if (_kafkaConsumerCallbackOut != nullptr) {
7850
delete _kafkaConsumerCallbackOut;
7951
_kafkaConsumerCallbackOut = nullptr;
@@ -86,25 +58,19 @@ void Aims::stop() {
8658
void Aims::initKafkaConsumer() {
8759
_kafkaConsumerCallbackOut = new aims::kafka::ConsumerOut(_context);
8860
std::vector<std::string> topicNames = adbase::explode(_configure->topicNameConsumerOut, ',', true);
89-
for(auto &t : topicNames) {
90-
adbase::kafka::Consumer* consumer = new adbase::kafka::Consumer(t, _configure->groupIdOut,
91-
_configure->brokerListConsumerOut);
92-
consumer->setMessageHandler(std::bind(&aims::kafka::ConsumerOut::pull,
93-
_kafkaConsumerCallbackOut,
94-
std::placeholders::_1, std::placeholders::_2,
95-
std::placeholders::_3, std::placeholders::_4));
96-
consumer->setStatCallback(std::bind(&aims::kafka::ConsumerOut::stat,
97-
_kafkaConsumerCallbackOut,
98-
std::placeholders::_1, std::placeholders::_2));
99-
consumer->setKafkaDebug(_configure->kafkaDebugOut);
100-
consumer->setOffsetStorePath(_configure->offsetPathOut);
101-
consumer->setKafkaStatInterval(_configure->statIntervalOut);
102-
if (_configure->isNewConsumerOut) {
103-
consumer->setIsNewConsumer(true);
104-
consumer->setOffsetStoreMethod("broker");
105-
}
106-
_kafkas[t] = consumer;
107-
}
61+
LOG_INFO << "Topic list:" << _configure->topicNameConsumerOut;
62+
63+
_kafka = new adbase::kafka::ConsumerBatch(topicNames, _configure->groupIdOut,
64+
_configure->brokerListConsumerOut);
65+
_kafka->setMessageHandler(std::bind(&aims::kafka::ConsumerOut::pull,
66+
_kafkaConsumerCallbackOut,
67+
std::placeholders::_1, std::placeholders::_2,
68+
std::placeholders::_3, std::placeholders::_4));
69+
_kafka->setStatCallback(std::bind(&aims::kafka::ConsumerOut::stat,
70+
_kafkaConsumerCallbackOut,
71+
std::placeholders::_1, std::placeholders::_2));
72+
_kafka->setKafkaDebug(_configure->kafkaDebugOut);
73+
_kafka->setKafkaStatInterval(_configure->statIntervalOut);
10874
}
10975

11076
// }}}

src/Aims.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Aims {
1212
Aims(AimsContext* context);
1313
~Aims();
1414
void run();
15+
void reload();
1516

1617
private:
1718
/// 传输上下文指针
@@ -22,7 +23,7 @@ class Aims {
2223
void stop();
2324
void initKafkaConsumer();
2425
aims::kafka::ConsumerOut* _kafkaConsumerCallbackOut = nullptr;
25-
std::unordered_map<std::string, adbase::kafka::Consumer*> _kafkas;
26+
adbase::kafka::ConsumerBatch* _kafka;
2627

2728
};
2829

src/Aims/Kafka/ConsumerOut.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ bool ConsumerOut::pull(const std::string& topicName, int partId, uint64_t offset
3333
// }}}
3434
// {{{ void ConsumerOut::stat()
3535

36-
void ConsumerOut::stat(adbase::kafka::Consumer* consumer, const std::string& stats) {
36+
void ConsumerOut::stat(adbase::kafka::ConsumerBatch*, const std::string& stats) {
3737
LOG_INFO << "Stats:" << stats.substr(0, 1024);
38-
LOG_INFO << consumer->getTopicName();
3938
}
4039

4140
// }}}

src/Aims/Kafka/ConsumerOut.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ class ConsumerOut {
1212
ConsumerOut(AimsContext* context);
1313
~ConsumerOut();
1414
bool pull(const std::string& topicName, int partId, uint64_t offset, const adbase::Buffer& data);
15-
void stat(adbase::kafka::Consumer* consumer, const std::string& stats);
15+
void stat(adbase::kafka::ConsumerBatch* consumer, const std::string& stats);
1616
private:
1717
AimsContext* _context;
1818
};
1919

2020
}
2121
}
22-
#endif
22+
#endif

src/App.cpp

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,18 @@
55

66
//{{{ macros
77

8-
#define LOAD_KAFKA_CONSUMER_CONFIG(name, sectionName) do {\
9-
_configure->isNewConsumer##name = config.getOptionBool("kafkac_"#sectionName, "isNewConsumer"#name);\
10-
_configure->topicNameConsumer##name = config.getOption("kafkac_"#sectionName, "topicName"#name);\
11-
_configure->groupId##name = config.getOption("kafkac_"#sectionName, "groupId"#name);\
12-
_configure->brokerListConsumer##name = config.getOption("kafkac_"#sectionName, "brokerList"#name);\
13-
_configure->kafkaDebug##name = config.getOption("kafkac_"#sectionName, "kafkaDebug"#name);\
14-
_configure->offsetPath##name = config.getOption("kafkac_"#sectionName, "offsetPath"#name);\
15-
_configure->statInterval##name = config.getOption("kafkac_"#sectionName, "statInterval"#name);\
16-
} while(0)
17-
18-
#define LOAD_KAFKA_PRODUCER_CONFIG(name, sectionName) do {\
19-
_configure->topicNameProducer##name = config.getOption("kafkap_"#sectionName, "topicName"#name);\
20-
_configure->brokerListProducer##name = config.getOption("kafkap_"#sectionName, "brokerList"#name);\
21-
_configure->debug##name = config.getOption("kafkap_"#sectionName, "debug"#name);\
22-
_configure->queueLength##name = config.getOptionUint32("kafkap_"#sectionName, "queueLength"#name);\
23-
} while(0)
24-
258
#define LOAD_TIMER_CONFIG(name) do {\
269
_configure->interval##name = config.getOptionUint32("timer", "interval"#name);\
2710
} while(0)
2811

12+
#define LOAD_KAFKA_CONSUMER_CONFIG(name, sectionName) do {\
13+
_configure->topicNameConsumer##name = config.getOption("kafkac_"#sectionName, "topicName"#name);\
14+
_configure->groupId##name = config.getOption("kafkac_"#sectionName, "groupId"#name);\
15+
_configure->brokerListConsumer##name = config.getOption("kafkac_"#sectionName, "brokerList"#name);\
16+
_configure->kafkaDebug##name = config.getOption("kafkac_"#sectionName, "kafkaDebug"#name);\
17+
_configure->statInterval##name = config.getOption("kafkac_"#sectionName, "statInterval"#name);\
18+
} while(0)
19+
2920
//}}}
3021
// {{{ App::App()
3122

@@ -46,7 +37,9 @@ void App::run() {
4637
_storage = std::shared_ptr<app::Storage>(new app::Storage(_configure));
4738
_storage->init();
4839

49-
_message = std::shared_ptr<app::Message>(new app::Message(_configure, _storage.get()));
40+
_metrics = std::shared_ptr<app::Metrics>(new app::Metrics());
41+
42+
_message = std::shared_ptr<app::Message>(new app::Message(_configure, _storage.get(), _metrics.get()));
5043
_message->start();
5144
}
5245

@@ -71,6 +64,7 @@ void App::stop() {
7164
void App::setAdServerContext(AdServerContext* context) {
7265
context->app = this;
7366
context->storage = _storage.get();
67+
context->appMetrics = _metrics.get();
7468
}
7569

7670
// }}}
@@ -80,6 +74,7 @@ void App::setAimsContext(AimsContext* context) {
8074
context->app = this;
8175
context->message = _message.get();
8276
context->storage = _storage.get();
77+
context->appMetrics = _metrics.get();
8378
}
8479

8580
// }}}
@@ -88,14 +83,13 @@ void App::setAimsContext(AimsContext* context) {
8883
void App::setTimerContext(TimerContext* context) {
8984
context->app = this;
9085
context->storage = _storage.get();
86+
context->appMetrics = _metrics.get();
9187
}
9288

9389
// }}}
9490
//{{{ void App::loadConfig()
9591

9692
void App::loadConfig(adbase::IniConfig& config) {
97-
LOAD_KAFKA_CONSUMER_CONFIG(Out, out);
98-
9993
_configure->luaDebug = config.getOptionBool("lua", "debug");
10094
_configure->luaScriptPath = config.getOption("lua", "scriptPath");
10195
_configure->consumerScriptName = config.getOption("consumer", "scriptName");
@@ -105,6 +99,7 @@ void App::loadConfig(adbase::IniConfig& config) {
10599
_configure->messageSwp = config.getOption("consumer", "messageSwp");
106100
_configure->httpScriptName = config.getOption("http", "scriptName");
107101

102+
LOAD_KAFKA_CONSUMER_CONFIG(Out, out);
108103
LOAD_TIMER_CONFIG(ClearStorage);
109104
}
110105

src/App.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "AdbaseConfig.hpp"
77
#include "App/Message.hpp"
88
#include "App/Storage.hpp"
9+
#include "App/Metrics.hpp"
910

1011
class App {
1112
public:
@@ -24,6 +25,7 @@ class App {
2425
AdbaseConfig* _configure;
2526
std::shared_ptr<app::Storage> _storage;
2627
std::shared_ptr<app::Message> _message;
28+
std::shared_ptr<app::Metrics> _metrics;
2729
mutable std::mutex _mut;
2830
void bindLuaMessage();
2931
};

src/App/Message.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Message.hpp"
22
#include "Storage.hpp"
3+
#include "Metrics.hpp"
34
#include <fstream>
45
#include <adbase/Lua.hpp>
56

@@ -9,7 +10,9 @@ thread_local std::unordered_map<std::string, MessageItem> messageLuaMessages;
910

1011
// {{{ Message::Message()
1112

12-
Message::Message(AdbaseConfig* configure, Storage* storage): _configure(configure), _storage(storage) {
13+
Message::Message(AdbaseConfig* configure, Storage* storage, Metrics* metrics): _configure(configure),
14+
_storage(storage),
15+
_metrics(metrics) {
1316
}
1417

1518
// }}}
@@ -120,6 +123,7 @@ void Message::initLua() {
120123
clazz.addMethod("rollback", rollbackFn);
121124

122125
_storage->bindClass(messageLuaEngine.get());
126+
_metrics->bindClass(messageLuaEngine.get());
123127
}
124128

125129
// }}}

src/App/Message.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
namespace app {
1616
class Storage;
17+
class Metrics;
1718
// message queue
1819
typedef struct MessageItem {
1920
int partId;
@@ -29,7 +30,7 @@ typedef std::list<std::list<std::string>> MessageToLua;
2930

3031
class Message {
3132
public:
32-
Message(AdbaseConfig* configure, Storage* storage);
33+
Message(AdbaseConfig* configure, Storage* storage, Metrics* metrics);
3334
~Message();
3435
void start();
3536
void stop();
@@ -47,6 +48,7 @@ class Message {
4748

4849
AdbaseConfig *_configure;
4950
Storage* _storage;
51+
Metrics* _metrics;
5052
std::unordered_map<int, MessageQueue*> _queues;
5153

5254
void callMessage();

0 commit comments

Comments
 (0)