2323import java .util .Map ;
2424import java .util .stream .Collectors ;
2525import org .apache .ignite .IgniteCache ;
26+ import org .apache .ignite .IgniteCheckedException ;
2627import org .apache .ignite .IgniteSystemProperties ;
2728import org .apache .ignite .cache .affinity .Affinity ;
2829import org .apache .ignite .cluster .ClusterNode ;
2930import org .apache .ignite .configuration .IgniteConfiguration ;
3031import org .apache .ignite .internal .IgniteEx ;
32+ import org .apache .ignite .lang .IgniteBiPredicate ;
3133import org .apache .ignite .testframework .junits .common .GridCommonAbstractTest ;
3234import org .junit .Test ;
3335
3739 */
3840public class MdcAffinityBackupFilterSelfTest extends GridCommonAbstractTest {
3941 /** */
40- private static final int PARTS_CNT = 8 ;
42+ private static final int PARTS_CNT = 1024 ;
4143
4244 /** */
4345 private int backups ;
4446
4547 /** */
4648 private String [] dcIds ;
4749
50+ /** */
51+ private IgniteBiPredicate <ClusterNode , List <ClusterNode >> filter ;
52+
4853 /** {@inheritDoc} */
4954 @ Override protected IgniteConfiguration getConfiguration (String igniteInstanceName ) throws Exception {
5055 IgniteConfiguration cfg = super .getConfiguration (igniteInstanceName );
@@ -53,7 +58,7 @@ public class MdcAffinityBackupFilterSelfTest extends GridCommonAbstractTest {
5358 .setBackups (backups )
5459 .setAffinity (
5560 new RendezvousAffinityFunction (false , PARTS_CNT )
56- .setAffinityBackupFilter (new MdcAffinityBackupFilter ( dcIds . length , backups ) )));
61+ .setAffinityBackupFilter (filter )));
5762
5863 return cfg ;
5964 }
@@ -70,26 +75,28 @@ public class MdcAffinityBackupFilterSelfTest extends GridCommonAbstractTest {
7075 */
7176 @ Test
7277 public void testSingleDcDeploymentIsProhibited () {
73- dcIds = new String [] {"DC_0" };
74- System .setProperty (IgniteSystemProperties .IGNITE_DATA_CENTER_ID , dcIds [0 ]);
75-
76- backups = 1 ;
77- verifyGridFailsToStartWithMessage ("Number of datacenters must be at least 2." );
78+ verifyMdcAffinityBackupFilterValidation (1 , 1 , "Number of datacenters must be at least 2." );
7879 }
7980
8081 /**
8182 * Verifies that {@link MdcAffinityBackupFilter} enforces even number of partition copies per datacenter.
8283 */
8384 @ Test
84- public void testEvenNumberOfPartitionCopiesPerDcIsEnforced () {
85- dcIds = new String [] {"DC_0" , "DC_1" , "DC_2" };
86- System .setProperty (IgniteSystemProperties .IGNITE_DATA_CENTER_ID , dcIds [0 ]);
85+ public void testUniformNumberOfPartitionCopiesPerDcIsEnforced () {
86+ verifyMdcAffinityBackupFilterValidation (3 , 1 , "recommended value is 2" );
8787
88- backups = 1 ;
89- verifyGridFailsToStartWithMessage ("recommended value is 2" );
88+ verifyMdcAffinityBackupFilterValidation (3 , 7 , "recommended values are 5 and 8" );
89+ }
90+
91+ /** */
92+ private void verifyMdcAffinityBackupFilterValidation (int dcsNum , int backups , String msg ) {
93+ try {
94+ new MdcAffinityBackupFilter (dcsNum , backups );
95+ } catch (IllegalArgumentException argEx ) {
96+ String errMsg = argEx .getMessage ();
9097
91- backups = 7 ;
92- verifyGridFailsToStartWithMessage ( "recommended values are 5 and 8" );
98+ assertTrue ( errMsg . contains ( msg )) ;
99+ }
93100 }
94101
95102 /**
@@ -104,6 +111,7 @@ public void test2DcDistribution() throws Exception {
104111 dcIds = new String [] {"DC_0" , "DC_1" };
105112 int nodesPerDc = 4 ;
106113 backups = 3 ;
114+ filter = new MdcAffinityBackupFilter (dcIds .length , backups );
107115
108116 IgniteEx srv = startClusterAcrossDataCenters (dcIds , nodesPerDc );
109117
@@ -134,22 +142,50 @@ public void test3DcDistribution() throws Exception {
134142 dcIds = new String [] {"DC_0" , "DC_1" , "DC_2" };
135143 int nodesPerDc = 2 ;
136144 backups = 5 ;
145+ filter = new MdcAffinityBackupFilter (dcIds .length , backups );
137146
138147 IgniteEx srv = startClusterAcrossDataCenters (dcIds , 2 );
139148
140149 verifyDistributionProperties (srv , dcIds , nodesPerDc , backups );
141150 }
142151
143- /** */
144- private void verifyGridFailsToStartWithMessage (String msg ) {
152+ /**
153+ * Verifies that node is prohibited from joining cluster if its affinityBackupFilter configuration differs
154+ * from the one specified in the cluster.
155+ *
156+ * @throws Exception If failed.
157+ */
158+ @ Test
159+ public void testAffinityFilterConfigurationValidation () throws Exception {
160+ dcIds = new String [] {"DC_0" , "DC_1" };
161+ backups = 3 ;
162+ filter = new MdcAffinityBackupFilter (dcIds .length , backups );
163+ startGrid (0 );
164+
165+ filter = new ClusterNodeAttributeAffinityBackupFilter ("DC_ID" );
145166 try {
146- startGrid (0 );
147- } catch (IllegalArgumentException argEx ) {
148- String errMsg = argEx .getMessage ();
167+ startGrid (1 );
149168
150- assertTrue (errMsg .contains (msg ));
151- } catch (Exception e ) {
152- fail ("Unexpected exception was thrown: " + e );
169+ fail ("Expected exception was not thrown." );
170+ } catch (IgniteCheckedException e ) {
171+ String errMsg = e .getMessage ();
172+
173+ assertNotNull (errMsg );
174+
175+ assertTrue (errMsg .contains ("Affinity backup filter class mismatch" ));
176+ }
177+
178+ filter = new MdcAffinityBackupFilter (dcIds .length , backups + dcIds .length );
179+ try {
180+ startGrid (1 );
181+
182+ fail ("Expected exception was not thrown." );
183+ } catch (IgniteCheckedException e ) {
184+ String errMsg = e .getMessage ();
185+
186+ assertNotNull (errMsg );
187+
188+ assertTrue (errMsg .contains ("Affinity backup filter mismatch" ));
153189 }
154190 }
155191
0 commit comments