Use add_partition(Partition, ifNotExists, needResults)
(javadoc) ... which (if the 2nd argument is true
) will only create an partition if it doesn't already exist.
Alternatively, just use add_partition(Partition)
to add the partition without a test, and catch the AlreadyExistsException
if it occurs.
Any scheme that involves testing that an action is possible and then doing that action has a potential race condition. In between the "test" and the "do", some other agent (thread, client, whatever) could do an action (the same one or a different one) that will cause your attempt to fail.
So your current approach is not only ugly. It also has a potential race condition if partitions could be created by multiple agents.