Here is the sample code:
package experiments.ignite;
import java.util.Arrays;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteLogger;
import org.apache.ignite.IgniteSet;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.CollectionConfiguration;
import org.apache.ignite.lang.IgniteRunnable;
import org.apache.ignite.resources.IgniteInstanceResource;
import org.apache.ignite.resources.LoggerResource;
public class DistributedSetDemo {
public static void main(final String[] args) {
final Ignite ignite = Ignition.start();
final CollectionConfiguration collectionConfiguration = new
CollectionConfiguration();
collectionConfiguration.setCollocated(true); // 1
// collectionConfiguration.setBackups(1); // 2
System.out.printf("Started node %s\n",
ignite.cluster().localNode().id());
IgniteSet<Long> numbers = ignite.set("numbers", null);
if (numbers == null) {
numbers = ignite.set("numbers", collectionConfiguration);
}
System.out.printf("[local] Size of the numbers distributed set is
%d, and items are %s\n",
numbers.size(),
Arrays.toString(numbers.toArray(new Long[numbers.size()])));
System.out.printf("[local] Is collocated? %b\n",
numbers.collocated());
System.out.printf("[local] numbers contains 42? %b\n",
numbers.contains(42L));
numbers.affinityRun(new IgniteRunnable() {
@IgniteInstanceResource
private Ignite ignite;
@LoggerResource
private IgniteLogger logger;
@Override
public void run() {
System.out.println(String.format("[affinity] Running on %s
node", ignite.cluster().localNode().id()));
final IgniteSet<Long> affinityNumbers =
ignite.set("numbers", null);
System.out.printf("[affinity] Size of the numbers
distributed set is %d, and items are %s\n",
affinityNumbers.size(),
Arrays.toString(affinityNumbers.toArray(new
Long[affinityNumbers.size()])));
System.out.printf("[affinity] Is collocated? %b\n",
affinityNumbers.collocated());
System.out.printf("[affinity] numbers contains 42? %b\n",
affinityNumbers.contains(42L));
}
});
numbers.add(42L);
}
}
or here https://gist.github.com/zshamrock/ac5e907bf1b091ad05570f77ae1ba69f.
Below are different scenarios with the actual and expected behavior.
Scenario #1
 Run first instance
Started node 54e59a6cdc404038b225b7f8a0a343cf
[local] Size of the numbers distributed set is 0, and items are []
[local] Is collocated? true
[local] numbers contains 42? false
[affinity] Running on 54e59a6cdc404038b225b7f8a0a343cf node
[affinity] Size of the numbers distributed set is 0, and items are []
[affinity] Is collocated? true
[affinity] numbers contains 42? false
 Run second instance
(from first instance console)
[affinity] Running on 54e59a6cdc404038b225b7f8a0a343cf node
[affinity] Size of the numbers distributed set is 1, and items are [42]
[affinity] Is collocated? true
[affinity] numbers contains 42? true
(from second instance console)
Started node 83429a510e6f48d3a991b0fbbe6ac58e
[local] Size of the numbers distributed set is 0, and items are []
[local] Is collocated? true
[local] numbers contains 42? true
 Run third instance
(from first instance console)
[affinity] Running on 54e59a6cdc404038b225b7f8a0a343cf node
[affinity] Size of the numbers distributed set is 1, and items are [42]
[affinity] Is collocated? true
[affinity] numbers contains 42? true
(from third instance console)
Started node e844d3fce9c24bb38dabefe44281c96f
[local] Size of the numbers distributed set is 0, and items are []
[local] Is collocated? true
[local] numbers contains 42? true
 Stop first instance
 Run fourth instance
(from third instance console)
[affinity] Running on e844d3fce9c24bb38dabefe44281c96f node
[affinity] Size of the numbers distributed set is 0, and items are []
[affinity] Is collocated? true
[affinity] numbers contains 42? true
(from fourth instance console)
Started node b3aff35980eb4a26aa3af8ba16d248ee
[local] Size of the numbers distributed set is 0, and items are []
[local] Is collocated? true
[local] numbers contains 42? true
*Actual behavior*
See above
*Expected behavior*
1. As there is no backup configured, I have not expected Set ownership to be
taken by 3rd instance.
2. Even, if 3rd instance took the ownership of the Set, the output from the
affinity run is strange, as comparing to the original affinity output from
the 1st instance (which reports the correct size, and toArray()), 3rd
instance only reports "contains", but empty for size() and toArray(). Which
is weird.
3. I still have expected "local" access to the distributed set would return
proper size and toArray(). Which it doesn't. Also strange to see although
"contains" reports true for all the cases.
Scenario #2 (with backups enabled)
Same behavior as for the Scenario #1
Scenario #3
(do not know how to reproduce it all the time, but saw it multiple times)
 Run first instance
Started node 54e59a6cdc404038b225b7f8a0a343cf
[local] Size of the numbers distributed set is 0, and items are []
[local] Is collocated? true
[local] numbers contains 42? false
[affinity] Running on 54e59a6cdc404038b225b7f8a0a343cf node
[affinity] Size of the numbers distributed set is 0, and items are []
[affinity] Is collocated? true
[affinity] numbers contains 42? false
 Run second instance
(from first instance console)
[affinity] Running on 54e59a6cdc404038b225b7f8a0a343cf node
[affinity] Size of the numbers distributed set is 1, and items are [42]
[affinity] Is collocated? true
[affinity] numbers contains 42? true
(from second instance console)
Started node 83429a510e6f48d3a991b0fbbe6ac58e
*[local] Size of the numbers distributed set is 1, and items are [42]*!!!
[local] Is collocated? true
[local] numbers contains 42? true
 Run third instance
(from first instance console)
[affinity] Running on 54e59a6cdc404038b225b7f8a0a343cf node
[affinity] Size of the numbers distributed set is 1, and items are [42]
[affinity] Is collocated? true
[affinity] numbers contains 42? true
(from third instance console)
Started node e844d3fce9c24bb38dabefe44281c96f
*[local] Size of the numbers distributed set is 0, and items are []*!!!
[local] Is collocated? true
[local] numbers contains 42? true
*Actual behavior*
See above
*Expected behavior*
Strange to see 2nd instance has reported the correct size and toArray()
(again it is not always happening), while the 3rd instance goes back to
report nothing (0 for the size, and empty toArray()).

View this message in context: http://apacheigniteusers.70518.x6.nabble.com/Strangecollocateddistributedsetbehaviortp5643.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.
