1919package org .apache .pulsar .broker .service ;
2020
2121import com .google .common .collect .Sets ;
22-
2322import java .util .Optional ;
2423import java .util .concurrent .TimeUnit ;
25-
2624import lombok .Cleanup ;
25+ import lombok .extern .slf4j .Slf4j ;
2726import org .apache .bookkeeper .conf .ServerConfiguration ;
2827import org .apache .pulsar .broker .PulsarService ;
2928import org .apache .pulsar .broker .ServiceConfiguration ;
3231import org .apache .pulsar .client .api .Producer ;
3332import org .apache .pulsar .client .api .PulsarClient ;
3433import org .apache .pulsar .client .api .PulsarClientException ;
34+ import org .apache .pulsar .client .impl .MessageImpl ;
3535import org .apache .pulsar .common .policies .data .ClusterData ;
3636import org .apache .pulsar .common .policies .data .TenantInfoImpl ;
3737import org .apache .pulsar .zookeeper .LocalBookkeeperEnsemble ;
4141import org .testng .annotations .Test ;
4242
4343@ Test (groups = "broker" )
44+ @ Slf4j
4445public class MaxMessageSizeTest {
4546
4647 PulsarService pulsar ;
@@ -55,7 +56,7 @@ void setup() {
5556 try {
5657 bkEnsemble = new LocalBookkeeperEnsemble (3 , 0 , () -> 0 );
5758 ServerConfiguration conf = new ServerConfiguration ();
58- conf .setNettyMaxFrameSizeBytes (10 * 1024 * 1024 );
59+ conf .setNettyMaxFrameSizeBytes (10 * 1024 * 1024 + 10 * 1024 );
5960 bkEnsemble .startStandalone (conf , false );
6061
6162 configuration = new ServiceConfiguration ();
@@ -78,7 +79,8 @@ void setup() {
7879 admin = PulsarAdmin .builder ().serviceHttpUrl (url ).build ();
7980 admin .clusters ().createCluster ("max_message_test" , ClusterData .builder ().serviceUrl (url ).build ());
8081 admin .tenants ()
81- .createTenant ("test" , new TenantInfoImpl (Sets .newHashSet ("appid1" ), Sets .newHashSet ("max_message_test" )));
82+ .createTenant ("test" ,
83+ new TenantInfoImpl (Sets .newHashSet ("appid1" ), Sets .newHashSet ("max_message_test" )));
8284 admin .namespaces ().createNamespace ("test/message" , Sets .newHashSet ("max_message_test" ));
8385 } catch (Exception e ) {
8486 e .printStackTrace ();
@@ -101,8 +103,8 @@ public void testMaxMessageSetting() throws PulsarClientException {
101103 @ Cleanup
102104 PulsarClient client = PulsarClient .builder ().serviceUrl (pulsar .getBrokerServiceUrl ()).build ();
103105 String topicName = "persistent://test/message/topic1" ;
104- Producer producer = client .newProducer ().topic (topicName ).sendTimeout (60 , TimeUnit .SECONDS ).create ();
105- Consumer consumer = client .newConsumer ().topic (topicName ).subscriptionName ("test1" ).subscribe ();
106+ Producer < byte []> producer = client .newProducer ().topic (topicName ).sendTimeout (60 , TimeUnit .SECONDS ).create ();
107+ Consumer < byte []> consumer = client .newConsumer ().topic (topicName ).subscriptionName ("test1" ).subscribe ();
106108
107109 // less than 5MB message
108110
@@ -139,6 +141,14 @@ public void testMaxMessageSetting() throws PulsarClientException {
139141 byte [] consumerNewNormalMsg = consumer .receive ().getData ();
140142 Assert .assertEquals (newNormalMsg , consumerNewNormalMsg );
141143
144+ // 2MB metadata and 8 MB payload
145+ try {
146+ producer .newMessage ().keyBytes (new byte [2 * 1024 * 1024 ]).value (newNormalMsg ).send ();
147+ Assert .fail ("Shouldn't send out this message" );
148+ } catch (PulsarClientException e ) {
149+ //no-op
150+ }
151+
142152 // equals 10MB message
143153 byte [] newLimitMsg = new byte [10 * 1024 * 1024 ];
144154 try {
@@ -151,6 +161,79 @@ public void testMaxMessageSetting() throws PulsarClientException {
151161 consumer .unsubscribe ();
152162 consumer .close ();
153163 producer .close ();
164+ }
154165
166+ @ Test
167+ public void testNonBatchingMaxMessageSize () throws Exception {
168+ @ Cleanup
169+ PulsarClient client = PulsarClient .builder ().serviceUrl (pulsar .getBrokerServiceUrl ()).build ();
170+ String topicName = "persistent://test/message/testNonBatchingMaxMessageSize" ;
171+ @ Cleanup
172+ Producer <byte []> producer = client .newProducer ()
173+ .topic (topicName )
174+ .enableBatching (false )
175+ .sendTimeout (30 , TimeUnit .SECONDS ).create ();
176+ @ Cleanup
177+ Consumer <byte []> consumer = client .newConsumer ().topic (topicName ).subscriptionName ("test1" ).subscribe ();
178+
179+ byte [] data = new byte [8 * 1024 * 1024 ];
180+ try {
181+ producer .newMessage ().value (data ).send ();
182+ } catch (PulsarClientException e ) {
183+ Assert .fail ("Shouldn't have exception at here" , e );
184+ }
185+ Assert .assertEquals (consumer .receive ().getData (), data );
186+
187+ // 1MB metadata and 8 MB payload
188+ try {
189+ producer .newMessage ().property ("P" , new String (new byte [1024 * 1024 ])).value (data ).send ();
190+ } catch (PulsarClientException e ) {
191+ Assert .fail ("Shouldn't have exception at here" , e );
192+ }
193+ Assert .assertEquals (consumer .receive ().getData (), data );
194+
195+ // 2MB metadata and 8 MB payload, should fail.
196+ try {
197+ producer .newMessage ().property ("P" , new String (new byte [2 * 1024 * 1024 ])).value (data ).send ();
198+ Assert .fail ("Shouldn't send out this message" );
199+ } catch (PulsarClientException e ) {
200+ //no-op
201+ }
202+ }
203+
204+ @ Test
205+ public void testChunkingMaxMessageSize () throws Exception {
206+ @ Cleanup
207+ PulsarClient client = PulsarClient .builder ().serviceUrl (pulsar .getBrokerServiceUrl ()).build ();
208+ String topicName = "persistent://test/message/testChunkingMaxMessageSize" ;
209+ @ Cleanup
210+ Producer <byte []> producer = client .newProducer ()
211+ .topic (topicName )
212+ .enableBatching (false )
213+ .enableChunking (true )
214+ .sendTimeout (30 , TimeUnit .SECONDS ).create ();
215+ @ Cleanup
216+ Consumer <byte []> consumer = client .newConsumer ().topic (topicName ).subscriptionName ("test1" ).subscribe ();
217+
218+ // 12 MB payload, there should be 2 chunks
219+ byte [] data = new byte [12 * 1024 * 1024 ];
220+ try {
221+ producer .newMessage ().value (data ).send ();
222+ } catch (PulsarClientException e ) {
223+ Assert .fail ("Shouldn't have exception at here" , e );
224+ }
225+ MessageImpl <byte []> msg = (MessageImpl <byte []>) consumer .receive ();
226+ Assert .assertEquals (msg .getData (), data );
227+ Assert .assertEquals (msg .getMessageBuilder ().getNumChunksFromMsg (), 2 );
228+
229+ // 5MB metadata and 12 MB payload, there should be 3 chunks
230+ try {
231+ producer .newMessage ().property ("P" , new String (new byte [5 * 1024 * 1024 ])).value (data ).send ();
232+ } catch (PulsarClientException e ) {
233+ Assert .fail ("Shouldn't have exception at here" , e );
234+ }
235+ msg = (MessageImpl <byte []>) consumer .receive ();
236+ Assert .assertEquals (msg .getData (), data );
237+ Assert .assertEquals (msg .getMessageBuilder ().getNumChunksFromMsg (), 3 );
155238 }
156239}
0 commit comments