mage icon indicating copy to clipboard operation
mage copied to clipboard

Devs: STATIC filter and data problems

Open JayDi85 opened this issue 8 years ago • 32 comments

Static filters is not constant and can be changed anytime by other source code. Well, it's not only static filters problem, but other "static" data and code too (early example with mana choices).

Last example with broken tests was Emrakul, the Aeons Torn (issue #4389, fix #4401). It's work fine until some problem card load and apply dirty ability to add new predicates -- that's code change origin static filter too. Error in one match will affect ALL server's data (e.g. all other matches with same card/filter will be broken until restart):

Broken static filter with dirty data after batch of tests (look at newly added predicates): shot_180112_151110

How it can be modified - example: shot_180112_152937

How to solve that problem:

  1. Need to use filter.copy() all around code with filter modifications;
  2. Somehow "lock" filter changed and throw exception on add new data to it:
  • fix for static init code:
public static final FilterControlledPermanent FILTER_XXX = new FilterControlledPermanent();
static {
  FILTER_XXX.add(Predicates.not(new CardTypePredicate(CardType.LAND)));
  FILTER_XXX.lockForChanges(true);
}
  • fix to FilterImpl:
    protected boolean changesLocked = false;
     
    @Override
    public final Filter add(Predicate predicate) {

        if (this.changesLocked) {
            throw new IllegalStateException("Filter was locked for any changes");
        }

        predicates.add(predicate);
        return this;
    }

Results with broken cards on db loading after fixes applied: shot_180112_155913

JayDi85 avatar Jan 12 '18 12:01 JayDi85

Issues with your approaches:

  1. Need to use filter.copy() all around code; Yes, but it requires changes everywhere + there is possiblity to copy already modified filter...
  2. Somehow "lock" filter changed and throw exception on add new data to it It just overcomplicates implementation of Filters and it's bad design in overall.

I think better options are:

  1. Replace static fields with static methods producing new filter objects instead of returning shared instances.
  2. Prohibit modifications to Filter classes:
  • Remove methods that allow to change state of Filter or
  • Change signatures of methods so that changing state always produce new fresh instance of Filter (chaining) with change.

rsypen avatar Jan 12 '18 13:01 rsypen

Personally I'd go with Prohibit modifications to Filter classes because concept of Filter function is that it should not really allow state modifications because making change to Filter gives you completely new filter. That concept should be reflected in code.

rsypen avatar Jan 12 '18 13:01 rsypen

Prohibit modifications to Filter classes

You can't do it. Literally every cards use filter modification:

    private static final FilterCreaturePermanent filter = new FilterCreaturePermanent("Ally creatures you control");
    static {
        filter.add(new SubtypePredicate(SubType.ALLY));
        filter.add(new ControllerPredicate(TargetController.YOU));
    }

1.Need to use filter.copy() all around code; Yes, but it requires changes everywhere + there is possiblity to copy already modified filter...

Only some cards have dirty code and modify static filters.

JayDi85 avatar Jan 12 '18 13:01 JayDi85

Got your point, but why not write code like that:

private static final FilterCreaturePermanent filter = new FilterCreaturePermanent("Ally creatures you control", Arrays.list(new SubtypePredicate(SubType.ALLY)), new ControllerPredicate(TargetController.YOU));

and make all Filter classes immutable?

rsypen avatar Jan 12 '18 13:01 rsypen

I can understand that a static filter is 'extended' when a card class also has a static initializer that falsely makes use of that filter. Which cards do have this 'dirty code' ?

ingmargoudt avatar Jan 12 '18 13:01 ingmargoudt

Pucas Mischief

JayDi85 avatar Jan 12 '18 13:01 JayDi85

Also some filters is runtime constructed. Example with Raging River:

shot_180112_180008

JayDi85 avatar Jan 12 '18 14:01 JayDi85

Ahhh s***t !!! thats my bad!! I changed a code in TargetControlledPermanent.java wrong to use a static filter!! And this ripples down to TargetPermanent that has the filter as class attribute. So that explains why the filter is changed at runtime

ingmargoudt avatar Jan 12 '18 14:01 ingmargoudt

TargetControlledPermanent.java::42 and ::46. This should be a 'new FilterControlledPermanent()' instead of the Static Filter reference.

ingmargoudt avatar Jan 12 '18 14:01 ingmargoudt

The filter used in Raging river is fine, as that is local to the method and cleaned up afterwards by gc.

ingmargoudt avatar Jan 12 '18 14:01 ingmargoudt

I will revert the Static Filter reference in TargetControlledPermanent

ingmargoudt avatar Jan 12 '18 14:01 ingmargoudt

@ingmargoudt no, it's not your fault. It's a static filter at all. Don't change it... Arght, never mind. Anyway need to fix filters. Your code was correct with static use -- it's helped to find out the origin problem.

JayDi85 avatar Jan 12 '18 14:01 JayDi85

Speaking of Raging River... I see object comparison using == rather than .equals()...

ingmargoudt avatar Jan 12 '18 14:01 ingmargoudt

We can make filter class immutable and add method like:

createModificableCopy(): MutableFilter

and provide all methods that changes state to MutableFilter class. MutableFilter class will be wrapper for any Filter instance that will operate on copy of filter.

Example. MutableFilter mutable = StaticFilter.SOME_FILTER.createModificableCopy(); mutable.add(Predicate) if(...) { if(..) { mutable.add(Predicate) } } battlefield.count(mutable)

rsypen avatar Jan 12 '18 14:01 rsypen

The StaticFilters were introduced to reduce code duplication, so instead of new ..Filter() references we just used the StaticFilter instance.

@JayDi85 I do see now that the staticfilter I used was indeed correct, since it was never changed anyway... but: Pucas Mischief has an filter that extends from TargetControlledFilter, and changes that filter at runtime. Changing the TargetControlledFilter to use a new instance rather than a static filter will resolve that I think

ingmargoudt avatar Jan 12 '18 14:01 ingmargoudt

Same for 'TargetPermanent'

ingmargoudt avatar Jan 12 '18 14:01 ingmargoudt

@ingmargoudt, I understand why they were introduced but problem is that you want to have CONSTANTS. Static keyword doesn't make underlaying objects immutable and that's the problem. They can be changed at any time and lead to many weird issues and problems.

rsypen avatar Jan 12 '18 14:01 rsypen

they are defined as final static, did we falsely use that as a definition for a constant?

ingmargoudt avatar Jan 12 '18 14:01 ingmargoudt

@ingmargoudt What are the possible negative consequences of using == instead of .equals when comparing UUIDs? I've conducted dozens of tests with the card and it worked well with everything I threw at it. Could something break it this way?

Zzooouhh avatar Jan 12 '18 17:01 Zzooouhh

Broken cards was fixed (there was only 3 cards). I think it gives more stability to server and fix some issues with not working cards like "must select target, but ability ignored that and not apply".

JayDi85 avatar Jan 12 '18 17:01 JayDi85