You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@beam.apache.org by Eugene Kirpichov <ki...@google.com.INVALID> on 2017/02/08 01:15:30 UTC
Should you always have a separate PTransform class for a new transform?
Hello,
I was auditing Beam for violations of PTransform style guide
https://beam.apache.org/contribute/ptransform-style-guide/ and came across
another style point that deserves discussion.
Look at Count transform:
public static <T> Combine.Globally<T, Long> globally() {
return Combine.globally(new CountFn<T>());
}
public static <K, V> Combine.PerKey<K, V, Long> perKey() {
return Combine.<K, V, Long>perKey(new CountFn<V>());
}
public static <T> PerElement<T> perElement() {
return new PerElement<>();
}
I asked myself: should globally() and perKey() also define wrapper classes
- e.g. should it be "public static <T> Globally<T, Long> globally()" where
"Globally" is a new inner class of Count?
I argue that the answer is yes, but it's not clear-cut.
Cons:
- If we return a Combine.Globally, the user can use the features provided
by Combine.Globally - e.g. .withDefaults(), .withFanout(),
.asSingletonView().
Pros:
- Style consistency with other transforms. Almost all transforms have their
own class, and their factory functions return that class.
- Implementation can evolve. However, in case of Count, that is unlikely.
- ...Or is it? If the transform has a concrete class, then the runner can
intercept that class and e.g. provide an (even) more efficient
implementation of Count.Globally. This gets much more awkward if the runner
has to intercept every Combine and check whether it's combining using a
CountFn.
So, I propose to add this as a style guide rule, and solve the problem in
"Cons" by saying "Yeah, if you want the extra features of the transform
you're expanding into, you have to propagate them through the API of your
transform and delegate to the underlying transform manually".
Thoughts?
Re: Should you always have a separate PTransform class for a new transform?
Posted by Dan Halperin <dh...@google.com.INVALID>.
I'll agree with the "Cons" by referencing back to this thread:
https://lists.apache.org/thread.html/CAA8k_FLVcMx+tYksxdMCXXe9y_ZYohE4ovHT9f2Jb1wCkOb+OQ@mail.gmail.com
On Tue, Feb 7, 2017 at 5:15 PM, Eugene Kirpichov <
kirpichov@google.com.invalid> wrote:
> Hello,
>
> I was auditing Beam for violations of PTransform style guide
> https://beam.apache.org/contribute/ptransform-style-guide/ and came across
> another style point that deserves discussion.
>
> Look at Count transform:
>
> public static <T> Combine.Globally<T, Long> globally() {
> return Combine.globally(new CountFn<T>());
> }
>
> public static <K, V> Combine.PerKey<K, V, Long> perKey() {
> return Combine.<K, V, Long>perKey(new CountFn<V>());
> }
>
> public static <T> PerElement<T> perElement() {
> return new PerElement<>();
> }
>
> I asked myself: should globally() and perKey() also define wrapper classes
> - e.g. should it be "public static <T> Globally<T, Long> globally()" where
> "Globally" is a new inner class of Count?
>
> I argue that the answer is yes, but it's not clear-cut.
> Cons:
> - If we return a Combine.Globally, the user can use the features provided
> by Combine.Globally - e.g. .withDefaults(), .withFanout(),
> .asSingletonView().
> Pros:
> - Style consistency with other transforms. Almost all transforms have their
> own class, and their factory functions return that class.
> - Implementation can evolve. However, in case of Count, that is unlikely.
> - ...Or is it? If the transform has a concrete class, then the runner can
> intercept that class and e.g. provide an (even) more efficient
> implementation of Count.Globally. This gets much more awkward if the runner
> has to intercept every Combine and check whether it's combining using a
> CountFn.
>
> So, I propose to add this as a style guide rule, and solve the problem in
> "Cons" by saying "Yeah, if you want the extra features of the transform
> you're expanding into, you have to propagate them through the API of your
> transform and delegate to the underlying transform manually".
>
> Thoughts?
>
Re: Should you always have a separate PTransform class for a new transform?
Posted by Eugene Kirpichov <ki...@google.com.INVALID>.
The discussion has been reignited on
https://github.com/apache/beam/pull/2603 .
I can see the pretty strong argument for not replicating all the features
of Combine on the hypothetical helper transform like Count.Globally.
I guess, then, I like Robert's option of having an interface like
"CombiningPTransform" with all the builder methods like withoutDefaults
etc., and returning that from transforms like Count.globally(), so that for
now one can simply return Combine.globally() from it (which would of course
implement this interface) but if later the implementation changes, it can
change to a composite transform builder which would also implement this
interface.
Notes on the PR itself:
- The change above would be a backward-compatible change so it wouldn't
necessarily have to go in before Beam stable release.
- However, if the current PR is submitted in the current form (returning a
Combine.Globally), then afterwards this change *would* be incompatible and
would have to be done before stable release.
So I'd suggest to hold off the PR until we reach consensus here.
On Wed, Feb 8, 2017 at 2:09 PM Eugene Kirpichov <ki...@google.com>
wrote:
> I think the value in having Mean.perKey() in addition to Mean.combineFn()
> is that using Mean.perKey() does not require knowledge of the combine
> concept, so easier for users. Generally, when using Beam, to simply compute
> a count or a mean, you should not need to know about combine.
>
> On Wed, Feb 8, 2017 at 2:06 PM Robert Bradshaw <ro...@google.com.invalid>
> wrote:
>
>> On Wed, Feb 8, 2017 at 1:27 PM, Eugene Kirpichov <
>> kirpichov@google.com.invalid> wrote:
>>
>> > So... Would it be fair to say that everybody would be satisfied if we
>> > treated the "glorified combine" transforms (Sum, Count, Mean, Sample,
>> > Latest) the following way:
>> > - For each case, SDK must expose the relevant CombineFn as a static
>> factory
>> > function: e.g. Sum.ofIntegers(), Latest.of(), etc. [it may make sense to
>> > discuss the naming of these CombineFn factory functions so they are not
>> > confused with neighboring PTransform factory functions]
>> >
>>
>> Or just expose the CombineFn class itself, rather than a factory method to
>> construct it? As for naming (if we go for factories), maybe
>> Mean.combineFn().
>>
>>
>> > - Expose also a PTransform factory function, returning either a
>> > PTransform<InputT, OutputT> (if there's nothing to configure on the
>> > transform), or its concrete subclass (if there is something to
>> configure).
>> > Transition from PTransform to subclass is always possible in a
>> > backward-compatible way, so it's safe to err on the side of returning
>> > PTransform.
>> > - Do *not* return concrete types from the PTransform factory function
>> such
>> > as Combine.Globally - instead, if the user has an advanced use case and
>> > wants to configure the combine, they should apply Combine.globally()
>> > themselves to your exposed CombineFn.
>> >
>> > In particular, this means:
>> > - Sum, Mean stay unchanged
>> > - Count, Sample, Latest should additionally expose their CombineFn's:
>> > Count.of()? or how should we name them?
>> > - Count.globally() and Count.perKey() should be changed from returning
>> > Combine.Globally and Combine.PerKey to returning the more general type
>> > PTransform<..., ...>. Cases where the user relies on them returning a
>> > Combine should be changed to applying the Combine manually.
>> >
>> > Makes sense?
>> >
>>
>> So the value in Mean.perKey() is solely in the fact that it's pithier for
>> Combine.perKey(Mean.combineFn())? Or do we assume that the former could
>> possibly get a new implementation, but the latter should be used if
>> additional configuration is needed?
>>
>> On Tue, Feb 7, 2017 at 10:50 PM Dan Halperin <dhalperi@google.com.invalid
>> >
>> > wrote:
>> >
>> > > I am generally persuaded to at least change my number to something
>> like 0
>> > > :). These are pretty reasonable perspectives, especially pointing out
>> > that
>> > > withSideInputs is pretty useless in Count ;)
>> > >
>> > > On Tue, Feb 7, 2017 at 10:04 PM, Kenneth Knowles
>> <klk@google.com.invalid
>> > >
>> > > wrote:
>> > >
>> > > > On Tue, Feb 7, 2017 at 8:43 PM, Eugene Kirpichov <
>> > > >
>> > > > > kirpichov@google.com.invalid> wrote:
>> > > > > I must admit I didn't quite
>> > > > > understand the option of "implements CombiningTransform".
>> > > > >
>> > > >
>> > > > On Tue, Feb 7, 2017 at 9:04 PM, Robert Bradshaw
>> > > > <robertwb@google.com.invalid
>> > > > > wrote:
>> > > >
>> > > > > Sorry, I'll try to clarify. ... <<codez>>...
>> > > > >
>> > > >
>> > > > FWIW this is also what I meant by my 3b "HasACombineInsideIt". The
>> > > > difference between my suggestion and Robert's is that I used a self
>> > type,
>> > > > which is really not worth the trouble (in fact, it blocked me from
>> > > bringing
>> > > > this to fruition last time we had this same conversation).
>> > > >
>> > > > Kenn
>> > > >
>> > >
>> >
>>
>
Re: Should you always have a separate PTransform class for a new transform?
Posted by Eugene Kirpichov <ki...@google.com.INVALID>.
I think the value in having Mean.perKey() in addition to Mean.combineFn()
is that using Mean.perKey() does not require knowledge of the combine
concept, so easier for users. Generally, when using Beam, to simply compute
a count or a mean, you should not need to know about combine.
On Wed, Feb 8, 2017 at 2:06 PM Robert Bradshaw <ro...@google.com.invalid>
wrote:
> On Wed, Feb 8, 2017 at 1:27 PM, Eugene Kirpichov <
> kirpichov@google.com.invalid> wrote:
>
> > So... Would it be fair to say that everybody would be satisfied if we
> > treated the "glorified combine" transforms (Sum, Count, Mean, Sample,
> > Latest) the following way:
> > - For each case, SDK must expose the relevant CombineFn as a static
> factory
> > function: e.g. Sum.ofIntegers(), Latest.of(), etc. [it may make sense to
> > discuss the naming of these CombineFn factory functions so they are not
> > confused with neighboring PTransform factory functions]
> >
>
> Or just expose the CombineFn class itself, rather than a factory method to
> construct it? As for naming (if we go for factories), maybe
> Mean.combineFn().
>
>
> > - Expose also a PTransform factory function, returning either a
> > PTransform<InputT, OutputT> (if there's nothing to configure on the
> > transform), or its concrete subclass (if there is something to
> configure).
> > Transition from PTransform to subclass is always possible in a
> > backward-compatible way, so it's safe to err on the side of returning
> > PTransform.
> > - Do *not* return concrete types from the PTransform factory function
> such
> > as Combine.Globally - instead, if the user has an advanced use case and
> > wants to configure the combine, they should apply Combine.globally()
> > themselves to your exposed CombineFn.
> >
> > In particular, this means:
> > - Sum, Mean stay unchanged
> > - Count, Sample, Latest should additionally expose their CombineFn's:
> > Count.of()? or how should we name them?
> > - Count.globally() and Count.perKey() should be changed from returning
> > Combine.Globally and Combine.PerKey to returning the more general type
> > PTransform<..., ...>. Cases where the user relies on them returning a
> > Combine should be changed to applying the Combine manually.
> >
> > Makes sense?
> >
>
> So the value in Mean.perKey() is solely in the fact that it's pithier for
> Combine.perKey(Mean.combineFn())? Or do we assume that the former could
> possibly get a new implementation, but the latter should be used if
> additional configuration is needed?
>
> On Tue, Feb 7, 2017 at 10:50 PM Dan Halperin <dh...@google.com.invalid>
> > wrote:
> >
> > > I am generally persuaded to at least change my number to something
> like 0
> > > :). These are pretty reasonable perspectives, especially pointing out
> > that
> > > withSideInputs is pretty useless in Count ;)
> > >
> > > On Tue, Feb 7, 2017 at 10:04 PM, Kenneth Knowles
> <klk@google.com.invalid
> > >
> > > wrote:
> > >
> > > > On Tue, Feb 7, 2017 at 8:43 PM, Eugene Kirpichov <
> > > >
> > > > > kirpichov@google.com.invalid> wrote:
> > > > > I must admit I didn't quite
> > > > > understand the option of "implements CombiningTransform".
> > > > >
> > > >
> > > > On Tue, Feb 7, 2017 at 9:04 PM, Robert Bradshaw
> > > > <robertwb@google.com.invalid
> > > > > wrote:
> > > >
> > > > > Sorry, I'll try to clarify. ... <<codez>>...
> > > > >
> > > >
> > > > FWIW this is also what I meant by my 3b "HasACombineInsideIt". The
> > > > difference between my suggestion and Robert's is that I used a self
> > type,
> > > > which is really not worth the trouble (in fact, it blocked me from
> > > bringing
> > > > this to fruition last time we had this same conversation).
> > > >
> > > > Kenn
> > > >
> > >
> >
>
Re: Should you always have a separate PTransform class for a new transform?
Posted by Robert Bradshaw <ro...@google.com.INVALID>.
On Wed, Feb 8, 2017 at 1:27 PM, Eugene Kirpichov <
kirpichov@google.com.invalid> wrote:
> So... Would it be fair to say that everybody would be satisfied if we
> treated the "glorified combine" transforms (Sum, Count, Mean, Sample,
> Latest) the following way:
> - For each case, SDK must expose the relevant CombineFn as a static factory
> function: e.g. Sum.ofIntegers(), Latest.of(), etc. [it may make sense to
> discuss the naming of these CombineFn factory functions so they are not
> confused with neighboring PTransform factory functions]
>
Or just expose the CombineFn class itself, rather than a factory method to
construct it? As for naming (if we go for factories), maybe
Mean.combineFn().
> - Expose also a PTransform factory function, returning either a
> PTransform<InputT, OutputT> (if there's nothing to configure on the
> transform), or its concrete subclass (if there is something to configure).
> Transition from PTransform to subclass is always possible in a
> backward-compatible way, so it's safe to err on the side of returning
> PTransform.
> - Do *not* return concrete types from the PTransform factory function such
> as Combine.Globally - instead, if the user has an advanced use case and
> wants to configure the combine, they should apply Combine.globally()
> themselves to your exposed CombineFn.
>
> In particular, this means:
> - Sum, Mean stay unchanged
> - Count, Sample, Latest should additionally expose their CombineFn's:
> Count.of()? or how should we name them?
> - Count.globally() and Count.perKey() should be changed from returning
> Combine.Globally and Combine.PerKey to returning the more general type
> PTransform<..., ...>. Cases where the user relies on them returning a
> Combine should be changed to applying the Combine manually.
>
> Makes sense?
>
So the value in Mean.perKey() is solely in the fact that it's pithier for
Combine.perKey(Mean.combineFn())? Or do we assume that the former could
possibly get a new implementation, but the latter should be used if
additional configuration is needed?
On Tue, Feb 7, 2017 at 10:50 PM Dan Halperin <dh...@google.com.invalid>
> wrote:
>
> > I am generally persuaded to at least change my number to something like 0
> > :). These are pretty reasonable perspectives, especially pointing out
> that
> > withSideInputs is pretty useless in Count ;)
> >
> > On Tue, Feb 7, 2017 at 10:04 PM, Kenneth Knowles <klk@google.com.invalid
> >
> > wrote:
> >
> > > On Tue, Feb 7, 2017 at 8:43 PM, Eugene Kirpichov <
> > >
> > > > kirpichov@google.com.invalid> wrote:
> > > > I must admit I didn't quite
> > > > understand the option of "implements CombiningTransform".
> > > >
> > >
> > > On Tue, Feb 7, 2017 at 9:04 PM, Robert Bradshaw
> > > <robertwb@google.com.invalid
> > > > wrote:
> > >
> > > > Sorry, I'll try to clarify. ... <<codez>>...
> > > >
> > >
> > > FWIW this is also what I meant by my 3b "HasACombineInsideIt". The
> > > difference between my suggestion and Robert's is that I used a self
> type,
> > > which is really not worth the trouble (in fact, it blocked me from
> > bringing
> > > this to fruition last time we had this same conversation).
> > >
> > > Kenn
> > >
> >
>
Re: Should you always have a separate PTransform class for a new transform?
Posted by Eugene Kirpichov <ki...@google.com.INVALID>.
So... Would it be fair to say that everybody would be satisfied if we
treated the "glorified combine" transforms (Sum, Count, Mean, Sample,
Latest) the following way:
- For each case, SDK must expose the relevant CombineFn as a static factory
function: e.g. Sum.ofIntegers(), Latest.of(), etc. [it may make sense to
discuss the naming of these CombineFn factory functions so they are not
confused with neighboring PTransform factory functions]
- Expose also a PTransform factory function, returning either a
PTransform<InputT, OutputT> (if there's nothing to configure on the
transform), or its concrete subclass (if there is something to configure).
Transition from PTransform to subclass is always possible in a
backward-compatible way, so it's safe to err on the side of returning
PTransform.
- Do *not* return concrete types from the PTransform factory function such
as Combine.Globally - instead, if the user has an advanced use case and
wants to configure the combine, they should apply Combine.globally()
themselves to your exposed CombineFn.
In particular, this means:
- Sum, Mean stay unchanged
- Count, Sample, Latest should additionally expose their CombineFn's:
Count.of()? or how should we name them?
- Count.globally() and Count.perKey() should be changed from returning
Combine.Globally and Combine.PerKey to returning the more general type
PTransform<..., ...>. Cases where the user relies on them returning a
Combine should be changed to applying the Combine manually.
Makes sense?
On Tue, Feb 7, 2017 at 10:50 PM Dan Halperin <dh...@google.com.invalid>
wrote:
> I am generally persuaded to at least change my number to something like 0
> :). These are pretty reasonable perspectives, especially pointing out that
> withSideInputs is pretty useless in Count ;)
>
> On Tue, Feb 7, 2017 at 10:04 PM, Kenneth Knowles <kl...@google.com.invalid>
> wrote:
>
> > On Tue, Feb 7, 2017 at 8:43 PM, Eugene Kirpichov <
> >
> > > kirpichov@google.com.invalid> wrote:
> > > I must admit I didn't quite
> > > understand the option of "implements CombiningTransform".
> > >
> >
> > On Tue, Feb 7, 2017 at 9:04 PM, Robert Bradshaw
> > <robertwb@google.com.invalid
> > > wrote:
> >
> > > Sorry, I'll try to clarify. ... <<codez>>...
> > >
> >
> > FWIW this is also what I meant by my 3b "HasACombineInsideIt". The
> > difference between my suggestion and Robert's is that I used a self type,
> > which is really not worth the trouble (in fact, it blocked me from
> bringing
> > this to fruition last time we had this same conversation).
> >
> > Kenn
> >
>
Re: Should you always have a separate PTransform class for a new transform?
Posted by Dan Halperin <dh...@google.com.INVALID>.
I am generally persuaded to at least change my number to something like 0
:). These are pretty reasonable perspectives, especially pointing out that
withSideInputs is pretty useless in Count ;)
On Tue, Feb 7, 2017 at 10:04 PM, Kenneth Knowles <kl...@google.com.invalid>
wrote:
> On Tue, Feb 7, 2017 at 8:43 PM, Eugene Kirpichov <
>
> > kirpichov@google.com.invalid> wrote:
> > I must admit I didn't quite
> > understand the option of "implements CombiningTransform".
> >
>
> On Tue, Feb 7, 2017 at 9:04 PM, Robert Bradshaw
> <robertwb@google.com.invalid
> > wrote:
>
> > Sorry, I'll try to clarify. ... <<codez>>...
> >
>
> FWIW this is also what I meant by my 3b "HasACombineInsideIt". The
> difference between my suggestion and Robert's is that I used a self type,
> which is really not worth the trouble (in fact, it blocked me from bringing
> this to fruition last time we had this same conversation).
>
> Kenn
>
Re: Should you always have a separate PTransform class for a new transform?
Posted by Kenneth Knowles <kl...@google.com.INVALID>.
On Tue, Feb 7, 2017 at 8:43 PM, Eugene Kirpichov <
> kirpichov@google.com.invalid> wrote:
> I must admit I didn't quite
> understand the option of "implements CombiningTransform".
>
On Tue, Feb 7, 2017 at 9:04 PM, Robert Bradshaw <robertwb@google.com.invalid
> wrote:
> Sorry, I'll try to clarify. ... <<codez>>...
>
FWIW this is also what I meant by my 3b "HasACombineInsideIt". The
difference between my suggestion and Robert's is that I used a self type,
which is really not worth the trouble (in fact, it blocked me from bringing
this to fruition last time we had this same conversation).
Kenn
Re: Should you always have a separate PTransform class for a new transform?
Posted by Robert Bradshaw <ro...@google.com.INVALID>.
On Tue, Feb 7, 2017 at 8:43 PM, Eugene Kirpichov <
kirpichov@google.com.invalid> wrote:
> I like the idea of "if you're really just a Combine, then expose (only) the
> CombineFn".
>
> However, in case of Count, another argument is that Count.perElement()
> already returns a PerElement transform, and it'd be awkward if globally()
> and perKey() were only exposed as CombineFn's. In a sense, if you are or
> might be developing a family of transforms, creating transform wrapper
> classes is a common denominator.
>
> So we have 3 options for Count.globally() and .perKey():
> 1. Return Combine.Globally, Combine.PerKey transforms (status quo)
> 2. Create wrapper classes Count.Globally, Count.PerKey
> 3. Expose only the respective CombineFn and have users apply it themselves
>
> It sounds like I am in favor of a mix of 2 and 3; Dan (so far) is in favor
> of 1; Kenn and Ben and you in favor of 3?
I'm in favor of 3 as well. Count is a bit special as there is a per-element
variant.
I must admit I didn't quite
> understand the option of "implements CombiningTransform".
>
Sorry, I'll try to clarify.
interface CombiningTransform<PIn, POut> {
CombiningTransform<PIn, POut> withFanout();
}
class Combine.Globally<VI, VO>
extends PTrasnform<PCollection<VI>, PCollection<VO>
implements CombiningTransform <PCollection<VI>, PCollection<VO>> {
...
}
class Sum {
public static CombiningTransform<PCollection<Integer>,
PCollection<Integer>> globally() {
return Combine.Globally(new SumIntegersCombineFn());
}
}
This gives the flexibility of changing the implementation without the
drawback of having to create a new class or worrying about extending
methods. Another advantage is that non-pure combines (e.g.
Count.perElement(), RemoveDuplicates(), etc. could implement this interface
as well.
There are some to-be-worked-out details, such as the fact that
CombiningTransform can't extend PTransform as PTransform is an abstract
class, not an interface, that might make this messier, if not impossible.
> On Tue, Feb 7, 2017 at 8:38 PM Robert Bradshaw <robertwb@google.com.invalid
> >
> wrote:
>
> > On Tue, Feb 7, 2017 at 7:49 PM, Kenneth Knowles <kl...@google.com.invalid>
> > wrote:
> >
> > > I am +0.7 on this idea. My rationale is contained in this thread, but I
> > > thought I would paraphrase it anyhow:
> > >
> > > "You automatically get all the features of Combine" / "If you add a
> > feature
> > > to Combine you have to update all wrappers"
> > >
> > > 0. I have been in some of the mentioned historical discussions and was
> > > swayed enough to not bother trying to change things, but I don't
> actually
> > > think the abstraction breakage is worth the benefits. I think the
> status
> > > quo is hanging on because it is the status quo.
> > >
> > > 1. Any Combine wrapper should also be providing its CombineFn, for use
> > with
> > > state. So the user already has a good way to get the same result
> without
> > > the wrapper, e.g. Combine.globally(Mean.of()). There are reasons to
> > provide
> > > a factory method, such as abstracting away tweaking of defaults, but I
> > > don't know of any cases of this happening.
> > >
> >
> > +1, we need to also be exposing pure CombineFns.
> >
> >
> > > 2. Most interesting transforms will not just be Combine wrappers - we
> > have
> > > a lot of them because we try to write pithy examples so I bet this
> issue
> > is
> > > a bigger deal to us than to our users. I don't have data to back this
> up;
> > > maybe users do write a bunch of Combine wrappers, but IMO they should
> > spend
> > > their time elsewhere. I certainly would. So the burden of people
> updating
> > > their Combine wrappers is not compelling to me.
> > >
> >
> > Agree here too, I don't think it's worth encouraging creating an extra
> > suite of classes just to avoid the Combine.perKey(...) and
> > Combine.globally(...). Also, as options are added (like hot key fanout)
> > it's likely plumbing these options through will be inconsistently done
> (as
> > well as a lot of effort).
> >
> >
> > > 2a. Consider also Count.perElement(). It really is a glorified combine,
> > but
> > > none of the supposed benefits of automatic propagation of tuning knobs
> > can
> > > by accrue to it, because it is not _just_ a Combine.
> > >
> > > 3. Those transforms that really are just Combine wrappers will not
> > require
> > > update when there is a new feature added to Combine. A new performance
> > > tuning knob, perhaps, but as Eugene points out, new features like side
> > > inputs or access to pipeline options aren't automatically applicable
> even
> > > if your transform is a glorified CombineFn.
> > >
> > > 3a. And since the argument is only about universally applicable builder
> > > methods on Combine, how many more do you imagine we will be adding?
> > >
> > > 3b. You can use type hackery to ensure you don't forget a knob, along
> the
> > > lines of `CountPerElement implements HasACombineInsideIt<
> > > CountPerElement>`.
> > > This would actually be a step towards enabling e.g. hot key counting in
> > > Count.PerElement
> > >
> >
> > Yes, more below.
> >
> >
> > > 4. If a transform that is just a wrapper needs to evolve into more,
> then
> > > you have to make a backwards incompatible change because you broke your
> > > abstractions, and you incur all the manual programming cost anyhow.
> > >
> >
> > I was thinking that the Java-esque way of doing things is to provide an
> > interface, CombiningTransform<PIC, PCO>, which the CombinePerKey (and
> > CombineGlobally) implement. CombiningTransform would have methods like
> > withFanout that return a CombiningTransform of the same type. PTransforms
> > like PerElement and RemoveDuplicates could implement this as well,
> despite
> > not being subclasses of the Combine ones, and delegate to their inner
> > Combine(s) as makes sense. Factory methods like Sum.integersGlobally()
> > would be declared to return a CombiningTransform of the right type, and
> the
> > default implementation would be to return a CombineGlobally but there is
> no
> > lock in (other than to support the CombiningTransform methods).
> >
> > If I were to write a style guide, I might summarize as:
> > >
> > > (a) If you are writing an interesting CombineFn, give it some public
> > > factory methods.
> > >
> > > (b) If you are writing a transform that _by definition, forever and
> > always_
> > > is a wrapper on Combine, stop after (a)
> > >
> > > (c) If you are writing a transform that has conceivably multiple
> > > implementation choices, you need an abstraction boundary to protect
> > against
> > > the current decision.
> > >
> > > (d) Only return a class more specific than PTransform if your transform
> > has
> > > more-specific methods to call.
> > >
> > >
> > > All that said, I'm not sure how much influence this particular
> collection
> > > of guidelines will have, hence I don't feel a full +1. (except (a)
> which
> > is
> > > very important).
> > >
> >
> > Your summary sounds more like a -1 to the proposal.
> >
> >
> > > On Tue, Feb 7, 2017 at 5:59 PM, Eugene Kirpichov <
> > > kirpichov@google.com.invalid> wrote:
> > >
> > > > There's 2 points here
> > > >
> > > > 1. If Count.globally() is implemented via Combine.globally(), then
> > should
> > > > Count.globally() return a Combine.Globally, or should it wrap it
> into a
> > > new
> > > > class Count.Globally? (that's what I'm wondering in this thread)
> > > >
> > > > I think the "least visibility" argument here would lead us to saying
> we
> > > > should wrap into a new class, because returning a Combine.Globally
> > leaks
> > > > the implementation detail that Count is always implemented via
> Combine.
> > > >
> > > > 2. If Thumbs.twiddle() transforms Foo to Bar, should it return a
> > > > PTransform<Foo, Bar>, or should it return Thumbs.Twiddle (which
> extends
> > > > PTransform<Foo, Bar>)? This is where your "least visibility" argument
> > > from
> > > > that thread applies more - we're exposing a more specific type - but
> I
> > > > think by exposing this type we are not imposing any additional
> > > obligations
> > > > on ourselves and not leaking any details: saying "twiddle() returns a
> > > > Twiddle" is as good as saying nothing.
> > > >
> > > > In case of Combine, the ability to additionally configure it for the
> > user
> > > > is a strong argument. But I think we've been through a similar
> > situation
> > > > and made a different decision: bounded reads from unbounded sources.
> We
> > > > started off with exposing UnboundedSource because a user might want
> to
> > > > specify .withMaxNumRecords() and .withMaxReadTime() on it, but
> changed
> > > our
> > > > minds to saying "if you're developing an unbounded connector, and if
> > you
> > > > want to make a promise that this is part of your connector's public
> > API,
> > > > then you have to implement these functions yourself". The reasoning
> was
> > > > that it might not stay an UnboundedSource forever - and indeed, with
> > SDF,
> > > > it won't, and the implementation of these functions will become
> custom.
> > > >
> > > > Also, the choice to return a Count.Globally (and propagate the needed
> > > > functions from Combine.Globally) makes the assumption that as a
> > developer
> > > > of the Count.Globally transform, you fully control its API surface.
> > E.g.
> > > by
> > > > supporting a .withFanout() function, you're saying "When counting
> > things
> > > > globally, you may need to configure the fanout". Corollary: you also
> > > > control what functions its API *doesn't* have - e.g. it probably
> > > shouldn't
> > > > have .withSideInputs() because side inputs make no sense here.
> > > >
> > > > The choice to return a Combine.Globally lets you automatically get
> all
> > > new
> > > > APIs that Combine.Globally may ever get - which may or may not be a
> > good
> > > > thing. It is good because these functions may be useful; it is bad
> > > because
> > > > as a developer of Count.Globally it is now your responsibility to
> > > > anticipate all possible interactions of parameters users might set on
> > it
> > > > with the particular CombineFn you supplied; and it is your
> > responsibility
> > > > to keep that up-to-date as Combine.Globally evolves (e.g. it might
> some
> > > day
> > > > add a parameter that interacts badly with CountFn and then you
> probably
> > > > should warn users not to set it). Granted, in this particular case
> it's
> > > > easy, but we're talking about the general principle.
> > > >
> > > > On Tue, Feb 7, 2017 at 5:45 PM Dan Halperin
> > <dhalperi@google.com.invalid
> > > >
> > > > wrote:
> > > >
> > > > > A little bit more inline:
> > > > >
> > > > > On Tue, Feb 7, 2017 at 5:15 PM, Eugene Kirpichov <
> > > > > kirpichov@google.com.invalid> wrote:
> > > > >
> > > > > > Hello,
> > > > > >
> > > > > > I was auditing Beam for violations of PTransform style guide
> > > > > > https://beam.apache.org/contribute/ptransform-style-guide/ and
> > came
> > > > > across
> > > > > > another style point that deserves discussion.
> > > > > >
> > > > > > Look at Count transform:
> > > > > >
> > > > > > public static <T> Combine.Globally<T, Long> globally() {
> > > > > > return Combine.globally(new CountFn<T>());
> > > > > > }
> > > > > >
> > > > > > public static <K, V> Combine.PerKey<K, V, Long> perKey() {
> > > > > > return Combine.<K, V, Long>perKey(new CountFn<V>());
> > > > > > }
> > > > > >
> > > > > > public static <T> PerElement<T> perElement() {
> > > > > > return new PerElement<>();
> > > > > > }
> > > > > >
> > > > > > I asked myself: should globally() and perKey() also define
> wrapper
> > > > > classes
> > > > > > - e.g. should it be "public static <T> Globally<T, Long>
> > globally()"
> > > > > where
> > > > > > "Globally" is a new inner class of Count?
> > > > > >
> > > > > > I argue that the answer is yes, but it's not clear-cut.
> > > > > > Cons:
> > > > > > - If we return a Combine.Globally, the user can use the features
> > > > provided
> > > > > > by Combine.Globally - e.g. .withDefaults(), .withFanout(),
> > > > > > .asSingletonView().
> > > > > >
> > > > >
> > > > > +1 to this point, which was a conscious decision in the pre-Beam
> days
> > > > > (which of course means it IS worth revisiting ;).
> > > > > Trying to replay the reasoning:
> > > > >
> > > > > * If wrapping, the author of a new Count.Globally can now only make
> > the
> > > > > extra functionality in Combine available by similarly exposing all
> > such
> > > > > functions.
> > > > >
> > > > > * Conversely, the current implementation makes new functionality in
> > > > Combine
> > > > > available "for free" to users of Count.globally(). Whereas new
> > > > > functionality on Combine would otherwise mandate that *all
> wrappers*
> > > > change
> > > > > to actually be exposed.
> > > > >
> > > > > * There's almost no data here, but: we have added new functionality
> > to
> > > > > Combine (withSideInputs) and have not added new functionality to
> > Count.
> > > > >
> > > > >
> > > > > > Pros:
> > > > > > - Style consistency with other transforms. Almost all transforms
> > have
> > > > > their
> > > > > > own class, and their factory functions return that class.
> > > > > >
> > > > >
> > > > > IMO this should only happen if the user needs that class. For all
> > > > examples
> > > > > I'm aware of,
> > > > > the returned class has stuff you need to do, like configuring
> coders
> > or
> > > > > side inputs or other parameters.
> > > > > IMO if the user need not configure, return the least constraining
> > thing
> > > > you
> > > > > can.
> > > > >
> > > > >
> > > > > > - Implementation can evolve. However, in case of Count, that is
> > > > unlikely.
> > > > > >
> > > > >
> > > > > +1 to "unlikely"
> > > > >
> > > > >
> > > > > > - ...Or is it? If the transform has a concrete class, then the
> > runner
> > > > can
> > > > > > intercept that class and e.g. provide an (even) more efficient
> > > > > > implementation of Count.Globally. This gets much more awkward if
> > the
> > > > > runner
> > > > > > has to intercept every Combine and check whether it's combining
> > > using a
> > > > > > CountFn.
> > > > > >
> > > > >
> > > > > Post-runner API, this argument goes away. Instead, the runner will
> > need
> > > > to
> > > > > inspect generically the attributes of the transform to do the
> > > > replacement.
> > > > >
> > > > > Summarizing: I currently am a -0.8 on this proposal.
> > > > >
> > > > >
> > > > > >
> > > > > > So, I propose to add this as a style guide rule, and solve the
> > > problem
> > > > in
> > > > > > "Cons" by saying "Yeah, if you want the extra features of the
> > > transform
> > > > > > you're expanding into, you have to propagate them through the API
> > of
> > > > your
> > > > > > transform and delegate to the underlying transform manually".
> > > > > >
> > > > > > Thoughts?
> > > > > >
> > > > >
> > > >
> > >
> >
>
Re: Should you always have a separate PTransform class for a new transform?
Posted by Kenneth Knowles <kl...@google.com.INVALID>.
On Tue, Feb 7, 2017 at 8:43 PM, Eugene Kirpichov <
kirpichov@google.com.invalid> wrote:
> So we have 3 options for Count.globally() and .perKey():
> 1. Return Combine.Globally, Combine.PerKey transforms (status quo)
>
This is not a full picture of the status quo. Count.perElement() returns a
PerElement class. Just saying this to make it clear that there is not one
consistent thing going on there.
Re: Should you always have a separate PTransform class for a new transform?
Posted by Eugene Kirpichov <ki...@google.com.INVALID>.
I like the idea of "if you're really just a Combine, then expose (only) the
CombineFn".
However, in case of Count, another argument is that Count.perElement()
already returns a PerElement transform, and it'd be awkward if globally()
and perKey() were only exposed as CombineFn's. In a sense, if you are or
might be developing a family of transforms, creating transform wrapper
classes is a common denominator.
So we have 3 options for Count.globally() and .perKey():
1. Return Combine.Globally, Combine.PerKey transforms (status quo)
2. Create wrapper classes Count.Globally, Count.PerKey
3. Expose only the respective CombineFn and have users apply it themselves
It sounds like I am in favor of a mix of 2 and 3; Dan (so far) is in favor
of 1; Kenn and Ben and you in favor of 3? I must admit I didn't quite
understand the option of "implements CombiningTransform".
On Tue, Feb 7, 2017 at 8:38 PM Robert Bradshaw <ro...@google.com.invalid>
wrote:
> On Tue, Feb 7, 2017 at 7:49 PM, Kenneth Knowles <kl...@google.com.invalid>
> wrote:
>
> > I am +0.7 on this idea. My rationale is contained in this thread, but I
> > thought I would paraphrase it anyhow:
> >
> > "You automatically get all the features of Combine" / "If you add a
> feature
> > to Combine you have to update all wrappers"
> >
> > 0. I have been in some of the mentioned historical discussions and was
> > swayed enough to not bother trying to change things, but I don't actually
> > think the abstraction breakage is worth the benefits. I think the status
> > quo is hanging on because it is the status quo.
> >
> > 1. Any Combine wrapper should also be providing its CombineFn, for use
> with
> > state. So the user already has a good way to get the same result without
> > the wrapper, e.g. Combine.globally(Mean.of()). There are reasons to
> provide
> > a factory method, such as abstracting away tweaking of defaults, but I
> > don't know of any cases of this happening.
> >
>
> +1, we need to also be exposing pure CombineFns.
>
>
> > 2. Most interesting transforms will not just be Combine wrappers - we
> have
> > a lot of them because we try to write pithy examples so I bet this issue
> is
> > a bigger deal to us than to our users. I don't have data to back this up;
> > maybe users do write a bunch of Combine wrappers, but IMO they should
> spend
> > their time elsewhere. I certainly would. So the burden of people updating
> > their Combine wrappers is not compelling to me.
> >
>
> Agree here too, I don't think it's worth encouraging creating an extra
> suite of classes just to avoid the Combine.perKey(...) and
> Combine.globally(...). Also, as options are added (like hot key fanout)
> it's likely plumbing these options through will be inconsistently done (as
> well as a lot of effort).
>
>
> > 2a. Consider also Count.perElement(). It really is a glorified combine,
> but
> > none of the supposed benefits of automatic propagation of tuning knobs
> can
> > by accrue to it, because it is not _just_ a Combine.
> >
> > 3. Those transforms that really are just Combine wrappers will not
> require
> > update when there is a new feature added to Combine. A new performance
> > tuning knob, perhaps, but as Eugene points out, new features like side
> > inputs or access to pipeline options aren't automatically applicable even
> > if your transform is a glorified CombineFn.
> >
> > 3a. And since the argument is only about universally applicable builder
> > methods on Combine, how many more do you imagine we will be adding?
> >
> > 3b. You can use type hackery to ensure you don't forget a knob, along the
> > lines of `CountPerElement implements HasACombineInsideIt<
> > CountPerElement>`.
> > This would actually be a step towards enabling e.g. hot key counting in
> > Count.PerElement
> >
>
> Yes, more below.
>
>
> > 4. If a transform that is just a wrapper needs to evolve into more, then
> > you have to make a backwards incompatible change because you broke your
> > abstractions, and you incur all the manual programming cost anyhow.
> >
>
> I was thinking that the Java-esque way of doing things is to provide an
> interface, CombiningTransform<PIC, PCO>, which the CombinePerKey (and
> CombineGlobally) implement. CombiningTransform would have methods like
> withFanout that return a CombiningTransform of the same type. PTransforms
> like PerElement and RemoveDuplicates could implement this as well, despite
> not being subclasses of the Combine ones, and delegate to their inner
> Combine(s) as makes sense. Factory methods like Sum.integersGlobally()
> would be declared to return a CombiningTransform of the right type, and the
> default implementation would be to return a CombineGlobally but there is no
> lock in (other than to support the CombiningTransform methods).
>
> If I were to write a style guide, I might summarize as:
> >
> > (a) If you are writing an interesting CombineFn, give it some public
> > factory methods.
> >
> > (b) If you are writing a transform that _by definition, forever and
> always_
> > is a wrapper on Combine, stop after (a)
> >
> > (c) If you are writing a transform that has conceivably multiple
> > implementation choices, you need an abstraction boundary to protect
> against
> > the current decision.
> >
> > (d) Only return a class more specific than PTransform if your transform
> has
> > more-specific methods to call.
> >
> >
> > All that said, I'm not sure how much influence this particular collection
> > of guidelines will have, hence I don't feel a full +1. (except (a) which
> is
> > very important).
> >
>
> Your summary sounds more like a -1 to the proposal.
>
>
> > On Tue, Feb 7, 2017 at 5:59 PM, Eugene Kirpichov <
> > kirpichov@google.com.invalid> wrote:
> >
> > > There's 2 points here
> > >
> > > 1. If Count.globally() is implemented via Combine.globally(), then
> should
> > > Count.globally() return a Combine.Globally, or should it wrap it into a
> > new
> > > class Count.Globally? (that's what I'm wondering in this thread)
> > >
> > > I think the "least visibility" argument here would lead us to saying we
> > > should wrap into a new class, because returning a Combine.Globally
> leaks
> > > the implementation detail that Count is always implemented via Combine.
> > >
> > > 2. If Thumbs.twiddle() transforms Foo to Bar, should it return a
> > > PTransform<Foo, Bar>, or should it return Thumbs.Twiddle (which extends
> > > PTransform<Foo, Bar>)? This is where your "least visibility" argument
> > from
> > > that thread applies more - we're exposing a more specific type - but I
> > > think by exposing this type we are not imposing any additional
> > obligations
> > > on ourselves and not leaking any details: saying "twiddle() returns a
> > > Twiddle" is as good as saying nothing.
> > >
> > > In case of Combine, the ability to additionally configure it for the
> user
> > > is a strong argument. But I think we've been through a similar
> situation
> > > and made a different decision: bounded reads from unbounded sources. We
> > > started off with exposing UnboundedSource because a user might want to
> > > specify .withMaxNumRecords() and .withMaxReadTime() on it, but changed
> > our
> > > minds to saying "if you're developing an unbounded connector, and if
> you
> > > want to make a promise that this is part of your connector's public
> API,
> > > then you have to implement these functions yourself". The reasoning was
> > > that it might not stay an UnboundedSource forever - and indeed, with
> SDF,
> > > it won't, and the implementation of these functions will become custom.
> > >
> > > Also, the choice to return a Count.Globally (and propagate the needed
> > > functions from Combine.Globally) makes the assumption that as a
> developer
> > > of the Count.Globally transform, you fully control its API surface.
> E.g.
> > by
> > > supporting a .withFanout() function, you're saying "When counting
> things
> > > globally, you may need to configure the fanout". Corollary: you also
> > > control what functions its API *doesn't* have - e.g. it probably
> > shouldn't
> > > have .withSideInputs() because side inputs make no sense here.
> > >
> > > The choice to return a Combine.Globally lets you automatically get all
> > new
> > > APIs that Combine.Globally may ever get - which may or may not be a
> good
> > > thing. It is good because these functions may be useful; it is bad
> > because
> > > as a developer of Count.Globally it is now your responsibility to
> > > anticipate all possible interactions of parameters users might set on
> it
> > > with the particular CombineFn you supplied; and it is your
> responsibility
> > > to keep that up-to-date as Combine.Globally evolves (e.g. it might some
> > day
> > > add a parameter that interacts badly with CountFn and then you probably
> > > should warn users not to set it). Granted, in this particular case it's
> > > easy, but we're talking about the general principle.
> > >
> > > On Tue, Feb 7, 2017 at 5:45 PM Dan Halperin
> <dhalperi@google.com.invalid
> > >
> > > wrote:
> > >
> > > > A little bit more inline:
> > > >
> > > > On Tue, Feb 7, 2017 at 5:15 PM, Eugene Kirpichov <
> > > > kirpichov@google.com.invalid> wrote:
> > > >
> > > > > Hello,
> > > > >
> > > > > I was auditing Beam for violations of PTransform style guide
> > > > > https://beam.apache.org/contribute/ptransform-style-guide/ and
> came
> > > > across
> > > > > another style point that deserves discussion.
> > > > >
> > > > > Look at Count transform:
> > > > >
> > > > > public static <T> Combine.Globally<T, Long> globally() {
> > > > > return Combine.globally(new CountFn<T>());
> > > > > }
> > > > >
> > > > > public static <K, V> Combine.PerKey<K, V, Long> perKey() {
> > > > > return Combine.<K, V, Long>perKey(new CountFn<V>());
> > > > > }
> > > > >
> > > > > public static <T> PerElement<T> perElement() {
> > > > > return new PerElement<>();
> > > > > }
> > > > >
> > > > > I asked myself: should globally() and perKey() also define wrapper
> > > > classes
> > > > > - e.g. should it be "public static <T> Globally<T, Long>
> globally()"
> > > > where
> > > > > "Globally" is a new inner class of Count?
> > > > >
> > > > > I argue that the answer is yes, but it's not clear-cut.
> > > > > Cons:
> > > > > - If we return a Combine.Globally, the user can use the features
> > > provided
> > > > > by Combine.Globally - e.g. .withDefaults(), .withFanout(),
> > > > > .asSingletonView().
> > > > >
> > > >
> > > > +1 to this point, which was a conscious decision in the pre-Beam days
> > > > (which of course means it IS worth revisiting ;).
> > > > Trying to replay the reasoning:
> > > >
> > > > * If wrapping, the author of a new Count.Globally can now only make
> the
> > > > extra functionality in Combine available by similarly exposing all
> such
> > > > functions.
> > > >
> > > > * Conversely, the current implementation makes new functionality in
> > > Combine
> > > > available "for free" to users of Count.globally(). Whereas new
> > > > functionality on Combine would otherwise mandate that *all wrappers*
> > > change
> > > > to actually be exposed.
> > > >
> > > > * There's almost no data here, but: we have added new functionality
> to
> > > > Combine (withSideInputs) and have not added new functionality to
> Count.
> > > >
> > > >
> > > > > Pros:
> > > > > - Style consistency with other transforms. Almost all transforms
> have
> > > > their
> > > > > own class, and their factory functions return that class.
> > > > >
> > > >
> > > > IMO this should only happen if the user needs that class. For all
> > > examples
> > > > I'm aware of,
> > > > the returned class has stuff you need to do, like configuring coders
> or
> > > > side inputs or other parameters.
> > > > IMO if the user need not configure, return the least constraining
> thing
> > > you
> > > > can.
> > > >
> > > >
> > > > > - Implementation can evolve. However, in case of Count, that is
> > > unlikely.
> > > > >
> > > >
> > > > +1 to "unlikely"
> > > >
> > > >
> > > > > - ...Or is it? If the transform has a concrete class, then the
> runner
> > > can
> > > > > intercept that class and e.g. provide an (even) more efficient
> > > > > implementation of Count.Globally. This gets much more awkward if
> the
> > > > runner
> > > > > has to intercept every Combine and check whether it's combining
> > using a
> > > > > CountFn.
> > > > >
> > > >
> > > > Post-runner API, this argument goes away. Instead, the runner will
> need
> > > to
> > > > inspect generically the attributes of the transform to do the
> > > replacement.
> > > >
> > > > Summarizing: I currently am a -0.8 on this proposal.
> > > >
> > > >
> > > > >
> > > > > So, I propose to add this as a style guide rule, and solve the
> > problem
> > > in
> > > > > "Cons" by saying "Yeah, if you want the extra features of the
> > transform
> > > > > you're expanding into, you have to propagate them through the API
> of
> > > your
> > > > > transform and delegate to the underlying transform manually".
> > > > >
> > > > > Thoughts?
> > > > >
> > > >
> > >
> >
>
Re: Should you always have a separate PTransform class for a new transform?
Posted by Robert Bradshaw <ro...@google.com.INVALID>.
On Tue, Feb 7, 2017 at 7:49 PM, Kenneth Knowles <kl...@google.com.invalid>
wrote:
> I am +0.7 on this idea. My rationale is contained in this thread, but I
> thought I would paraphrase it anyhow:
>
> "You automatically get all the features of Combine" / "If you add a feature
> to Combine you have to update all wrappers"
>
> 0. I have been in some of the mentioned historical discussions and was
> swayed enough to not bother trying to change things, but I don't actually
> think the abstraction breakage is worth the benefits. I think the status
> quo is hanging on because it is the status quo.
>
> 1. Any Combine wrapper should also be providing its CombineFn, for use with
> state. So the user already has a good way to get the same result without
> the wrapper, e.g. Combine.globally(Mean.of()). There are reasons to provide
> a factory method, such as abstracting away tweaking of defaults, but I
> don't know of any cases of this happening.
>
+1, we need to also be exposing pure CombineFns.
> 2. Most interesting transforms will not just be Combine wrappers - we have
> a lot of them because we try to write pithy examples so I bet this issue is
> a bigger deal to us than to our users. I don't have data to back this up;
> maybe users do write a bunch of Combine wrappers, but IMO they should spend
> their time elsewhere. I certainly would. So the burden of people updating
> their Combine wrappers is not compelling to me.
>
Agree here too, I don't think it's worth encouraging creating an extra
suite of classes just to avoid the Combine.perKey(...) and
Combine.globally(...). Also, as options are added (like hot key fanout)
it's likely plumbing these options through will be inconsistently done (as
well as a lot of effort).
> 2a. Consider also Count.perElement(). It really is a glorified combine, but
> none of the supposed benefits of automatic propagation of tuning knobs can
> by accrue to it, because it is not _just_ a Combine.
>
> 3. Those transforms that really are just Combine wrappers will not require
> update when there is a new feature added to Combine. A new performance
> tuning knob, perhaps, but as Eugene points out, new features like side
> inputs or access to pipeline options aren't automatically applicable even
> if your transform is a glorified CombineFn.
>
> 3a. And since the argument is only about universally applicable builder
> methods on Combine, how many more do you imagine we will be adding?
>
> 3b. You can use type hackery to ensure you don't forget a knob, along the
> lines of `CountPerElement implements HasACombineInsideIt<
> CountPerElement>`.
> This would actually be a step towards enabling e.g. hot key counting in
> Count.PerElement
>
Yes, more below.
> 4. If a transform that is just a wrapper needs to evolve into more, then
> you have to make a backwards incompatible change because you broke your
> abstractions, and you incur all the manual programming cost anyhow.
>
I was thinking that the Java-esque way of doing things is to provide an
interface, CombiningTransform<PIC, PCO>, which the CombinePerKey (and
CombineGlobally) implement. CombiningTransform would have methods like
withFanout that return a CombiningTransform of the same type. PTransforms
like PerElement and RemoveDuplicates could implement this as well, despite
not being subclasses of the Combine ones, and delegate to their inner
Combine(s) as makes sense. Factory methods like Sum.integersGlobally()
would be declared to return a CombiningTransform of the right type, and the
default implementation would be to return a CombineGlobally but there is no
lock in (other than to support the CombiningTransform methods).
If I were to write a style guide, I might summarize as:
>
> (a) If you are writing an interesting CombineFn, give it some public
> factory methods.
>
> (b) If you are writing a transform that _by definition, forever and always_
> is a wrapper on Combine, stop after (a)
>
> (c) If you are writing a transform that has conceivably multiple
> implementation choices, you need an abstraction boundary to protect against
> the current decision.
>
> (d) Only return a class more specific than PTransform if your transform has
> more-specific methods to call.
>
>
> All that said, I'm not sure how much influence this particular collection
> of guidelines will have, hence I don't feel a full +1. (except (a) which is
> very important).
>
Your summary sounds more like a -1 to the proposal.
> On Tue, Feb 7, 2017 at 5:59 PM, Eugene Kirpichov <
> kirpichov@google.com.invalid> wrote:
>
> > There's 2 points here
> >
> > 1. If Count.globally() is implemented via Combine.globally(), then should
> > Count.globally() return a Combine.Globally, or should it wrap it into a
> new
> > class Count.Globally? (that's what I'm wondering in this thread)
> >
> > I think the "least visibility" argument here would lead us to saying we
> > should wrap into a new class, because returning a Combine.Globally leaks
> > the implementation detail that Count is always implemented via Combine.
> >
> > 2. If Thumbs.twiddle() transforms Foo to Bar, should it return a
> > PTransform<Foo, Bar>, or should it return Thumbs.Twiddle (which extends
> > PTransform<Foo, Bar>)? This is where your "least visibility" argument
> from
> > that thread applies more - we're exposing a more specific type - but I
> > think by exposing this type we are not imposing any additional
> obligations
> > on ourselves and not leaking any details: saying "twiddle() returns a
> > Twiddle" is as good as saying nothing.
> >
> > In case of Combine, the ability to additionally configure it for the user
> > is a strong argument. But I think we've been through a similar situation
> > and made a different decision: bounded reads from unbounded sources. We
> > started off with exposing UnboundedSource because a user might want to
> > specify .withMaxNumRecords() and .withMaxReadTime() on it, but changed
> our
> > minds to saying "if you're developing an unbounded connector, and if you
> > want to make a promise that this is part of your connector's public API,
> > then you have to implement these functions yourself". The reasoning was
> > that it might not stay an UnboundedSource forever - and indeed, with SDF,
> > it won't, and the implementation of these functions will become custom.
> >
> > Also, the choice to return a Count.Globally (and propagate the needed
> > functions from Combine.Globally) makes the assumption that as a developer
> > of the Count.Globally transform, you fully control its API surface. E.g.
> by
> > supporting a .withFanout() function, you're saying "When counting things
> > globally, you may need to configure the fanout". Corollary: you also
> > control what functions its API *doesn't* have - e.g. it probably
> shouldn't
> > have .withSideInputs() because side inputs make no sense here.
> >
> > The choice to return a Combine.Globally lets you automatically get all
> new
> > APIs that Combine.Globally may ever get - which may or may not be a good
> > thing. It is good because these functions may be useful; it is bad
> because
> > as a developer of Count.Globally it is now your responsibility to
> > anticipate all possible interactions of parameters users might set on it
> > with the particular CombineFn you supplied; and it is your responsibility
> > to keep that up-to-date as Combine.Globally evolves (e.g. it might some
> day
> > add a parameter that interacts badly with CountFn and then you probably
> > should warn users not to set it). Granted, in this particular case it's
> > easy, but we're talking about the general principle.
> >
> > On Tue, Feb 7, 2017 at 5:45 PM Dan Halperin <dhalperi@google.com.invalid
> >
> > wrote:
> >
> > > A little bit more inline:
> > >
> > > On Tue, Feb 7, 2017 at 5:15 PM, Eugene Kirpichov <
> > > kirpichov@google.com.invalid> wrote:
> > >
> > > > Hello,
> > > >
> > > > I was auditing Beam for violations of PTransform style guide
> > > > https://beam.apache.org/contribute/ptransform-style-guide/ and came
> > > across
> > > > another style point that deserves discussion.
> > > >
> > > > Look at Count transform:
> > > >
> > > > public static <T> Combine.Globally<T, Long> globally() {
> > > > return Combine.globally(new CountFn<T>());
> > > > }
> > > >
> > > > public static <K, V> Combine.PerKey<K, V, Long> perKey() {
> > > > return Combine.<K, V, Long>perKey(new CountFn<V>());
> > > > }
> > > >
> > > > public static <T> PerElement<T> perElement() {
> > > > return new PerElement<>();
> > > > }
> > > >
> > > > I asked myself: should globally() and perKey() also define wrapper
> > > classes
> > > > - e.g. should it be "public static <T> Globally<T, Long> globally()"
> > > where
> > > > "Globally" is a new inner class of Count?
> > > >
> > > > I argue that the answer is yes, but it's not clear-cut.
> > > > Cons:
> > > > - If we return a Combine.Globally, the user can use the features
> > provided
> > > > by Combine.Globally - e.g. .withDefaults(), .withFanout(),
> > > > .asSingletonView().
> > > >
> > >
> > > +1 to this point, which was a conscious decision in the pre-Beam days
> > > (which of course means it IS worth revisiting ;).
> > > Trying to replay the reasoning:
> > >
> > > * If wrapping, the author of a new Count.Globally can now only make the
> > > extra functionality in Combine available by similarly exposing all such
> > > functions.
> > >
> > > * Conversely, the current implementation makes new functionality in
> > Combine
> > > available "for free" to users of Count.globally(). Whereas new
> > > functionality on Combine would otherwise mandate that *all wrappers*
> > change
> > > to actually be exposed.
> > >
> > > * There's almost no data here, but: we have added new functionality to
> > > Combine (withSideInputs) and have not added new functionality to Count.
> > >
> > >
> > > > Pros:
> > > > - Style consistency with other transforms. Almost all transforms have
> > > their
> > > > own class, and their factory functions return that class.
> > > >
> > >
> > > IMO this should only happen if the user needs that class. For all
> > examples
> > > I'm aware of,
> > > the returned class has stuff you need to do, like configuring coders or
> > > side inputs or other parameters.
> > > IMO if the user need not configure, return the least constraining thing
> > you
> > > can.
> > >
> > >
> > > > - Implementation can evolve. However, in case of Count, that is
> > unlikely.
> > > >
> > >
> > > +1 to "unlikely"
> > >
> > >
> > > > - ...Or is it? If the transform has a concrete class, then the runner
> > can
> > > > intercept that class and e.g. provide an (even) more efficient
> > > > implementation of Count.Globally. This gets much more awkward if the
> > > runner
> > > > has to intercept every Combine and check whether it's combining
> using a
> > > > CountFn.
> > > >
> > >
> > > Post-runner API, this argument goes away. Instead, the runner will need
> > to
> > > inspect generically the attributes of the transform to do the
> > replacement.
> > >
> > > Summarizing: I currently am a -0.8 on this proposal.
> > >
> > >
> > > >
> > > > So, I propose to add this as a style guide rule, and solve the
> problem
> > in
> > > > "Cons" by saying "Yeah, if you want the extra features of the
> transform
> > > > you're expanding into, you have to propagate them through the API of
> > your
> > > > transform and delegate to the underlying transform manually".
> > > >
> > > > Thoughts?
> > > >
> > >
> >
>
Re: Should you always have a separate PTransform class for a new transform?
Posted by Ben Chambers <bc...@google.com.INVALID>.
Going back to the beginning, why not "Combine.perKey(Count.fn())" or some
such?
We have a lot of boilerplate already to support "Count.perKey()" and that
will only become worse when we try to have a Count.PerKey class that has
the functionality of Combine.PerKey, why not just get rid of the
boilerplate?
Pithy examples aside, there seems to be value in less implementation
complexity and making it clear that Count is just a CombineFn.
On Tue, Feb 7, 2017, 7:49 PM Kenneth Knowles <kl...@google.com.invalid> wrote:
> I am +0.7 on this idea. My rationale is contained in this thread, but I
> thought I would paraphrase it anyhow:
>
>
> "You automatically get all the features of Combine" / "If you add a feature
> to Combine you have to update all wrappers"
>
> 0. I have been in some of the mentioned historical discussions and was
> swayed enough to not bother trying to change things, but I don't actually
> think the abstraction breakage is worth the benefits. I think the status
> quo is hanging on because it is the status quo.
>
> 1. Any Combine wrapper should also be providing its CombineFn, for use with
> state. So the user already has a good way to get the same result without
> the wrapper, e.g. Combine.globally(Mean.of()). There are reasons to provide
> a factory method, such as abstracting away tweaking of defaults, but I
> don't know of any cases of this happening.
>
> 2. Most interesting transforms will not just be Combine wrappers - we have
> a lot of them because we try to write pithy examples so I bet this issue is
> a bigger deal to us than to our users. I don't have data to back this up;
> maybe users do write a bunch of Combine wrappers, but IMO they should spend
> their time elsewhere. I certainly would. So the burden of people updating
> their Combine wrappers is not compelling to me.
>
> 2a. Consider also Count.perElement(). It really is a glorified combine, but
> none of the supposed benefits of automatic propagation of tuning knobs can
> by accrue to it, because it is not _just_ a Combine.
>
> 3. Those transforms that really are just Combine wrappers will not require
> update when there is a new feature added to Combine. A new performance
> tuning knob, perhaps, but as Eugene points out, new features like side
> inputs or access to pipeline options aren't automatically applicable even
> if your transform is a glorified CombineFn.
>
> 3a. And since the argument is only about universally applicable builder
> methods on Combine, how many more do you imagine we will be adding?
>
> 3b. You can use type hackery to ensure you don't forget a knob, along the
> lines of `CountPerElement implements HasACombineInsideIt<CountPerElement>`.
> This would actually be a step towards enabling e.g. hot key counting in
> Count.PerElement
>
> 4. If a transform that is just a wrapper needs to evolve into more, then
> you have to make a backwards incompatible change because you broke your
> abstractions, and you incur all the manual programming cost anyhow.
>
>
> If I were to write a style guide, I might summarize as:
>
> (a) If you are writing an interesting CombineFn, give it some public
> factory methods.
>
> (b) If you are writing a transform that _by definition, forever and always_
> is a wrapper on Combine, stop after (a)
>
> (c) If you are writing a transform that has conceivably multiple
> implementation choices, you need an abstraction boundary to protect against
> the current decision.
>
> (d) Only return a class more specific than PTransform if your transform has
> more-specific methods to call.
>
>
> All that said, I'm not sure how much influence this particular collection
> of guidelines will have, hence I don't feel a full +1. (except (a) which is
> very important).
>
> Kenn
>
> On Tue, Feb 7, 2017 at 5:59 PM, Eugene Kirpichov <
> kirpichov@google.com.invalid> wrote:
>
> > There's 2 points here
> >
> > 1. If Count.globally() is implemented via Combine.globally(), then should
> > Count.globally() return a Combine.Globally, or should it wrap it into a
> new
> > class Count.Globally? (that's what I'm wondering in this thread)
> >
> > I think the "least visibility" argument here would lead us to saying we
> > should wrap into a new class, because returning a Combine.Globally leaks
> > the implementation detail that Count is always implemented via Combine.
> >
> > 2. If Thumbs.twiddle() transforms Foo to Bar, should it return a
> > PTransform<Foo, Bar>, or should it return Thumbs.Twiddle (which extends
> > PTransform<Foo, Bar>)? This is where your "least visibility" argument
> from
> > that thread applies more - we're exposing a more specific type - but I
> > think by exposing this type we are not imposing any additional
> obligations
> > on ourselves and not leaking any details: saying "twiddle() returns a
> > Twiddle" is as good as saying nothing.
> >
> > In case of Combine, the ability to additionally configure it for the user
> > is a strong argument. But I think we've been through a similar situation
> > and made a different decision: bounded reads from unbounded sources. We
> > started off with exposing UnboundedSource because a user might want to
> > specify .withMaxNumRecords() and .withMaxReadTime() on it, but changed
> our
> > minds to saying "if you're developing an unbounded connector, and if you
> > want to make a promise that this is part of your connector's public API,
> > then you have to implement these functions yourself". The reasoning was
> > that it might not stay an UnboundedSource forever - and indeed, with SDF,
> > it won't, and the implementation of these functions will become custom.
> >
> > Also, the choice to return a Count.Globally (and propagate the needed
> > functions from Combine.Globally) makes the assumption that as a developer
> > of the Count.Globally transform, you fully control its API surface. E.g.
> by
> > supporting a .withFanout() function, you're saying "When counting things
> > globally, you may need to configure the fanout". Corollary: you also
> > control what functions its API *doesn't* have - e.g. it probably
> shouldn't
> > have .withSideInputs() because side inputs make no sense here.
> >
> > The choice to return a Combine.Globally lets you automatically get all
> new
> > APIs that Combine.Globally may ever get - which may or may not be a good
> > thing. It is good because these functions may be useful; it is bad
> because
> > as a developer of Count.Globally it is now your responsibility to
> > anticipate all possible interactions of parameters users might set on it
> > with the particular CombineFn you supplied; and it is your responsibility
> > to keep that up-to-date as Combine.Globally evolves (e.g. it might some
> day
> > add a parameter that interacts badly with CountFn and then you probably
> > should warn users not to set it). Granted, in this particular case it's
> > easy, but we're talking about the general principle.
> >
> > On Tue, Feb 7, 2017 at 5:45 PM Dan Halperin <dhalperi@google.com.invalid
> >
> > wrote:
> >
> > > A little bit more inline:
> > >
> > > On Tue, Feb 7, 2017 at 5:15 PM, Eugene Kirpichov <
> > > kirpichov@google.com.invalid> wrote:
> > >
> > > > Hello,
> > > >
> > > > I was auditing Beam for violations of PTransform style guide
> > > > https://beam.apache.org/contribute/ptransform-style-guide/ and came
> > > across
> > > > another style point that deserves discussion.
> > > >
> > > > Look at Count transform:
> > > >
> > > > public static <T> Combine.Globally<T, Long> globally() {
> > > > return Combine.globally(new CountFn<T>());
> > > > }
> > > >
> > > > public static <K, V> Combine.PerKey<K, V, Long> perKey() {
> > > > return Combine.<K, V, Long>perKey(new CountFn<V>());
> > > > }
> > > >
> > > > public static <T> PerElement<T> perElement() {
> > > > return new PerElement<>();
> > > > }
> > > >
> > > > I asked myself: should globally() and perKey() also define wrapper
> > > classes
> > > > - e.g. should it be "public static <T> Globally<T, Long> globally()"
> > > where
> > > > "Globally" is a new inner class of Count?
> > > >
> > > > I argue that the answer is yes, but it's not clear-cut.
> > > > Cons:
> > > > - If we return a Combine.Globally, the user can use the features
> > provided
> > > > by Combine.Globally - e.g. .withDefaults(), .withFanout(),
> > > > .asSingletonView().
> > > >
> > >
> > > +1 to this point, which was a conscious decision in the pre-Beam days
> > > (which of course means it IS worth revisiting ;).
> > > Trying to replay the reasoning:
> > >
> > > * If wrapping, the author of a new Count.Globally can now only make the
> > > extra functionality in Combine available by similarly exposing all such
> > > functions.
> > >
> > > * Conversely, the current implementation makes new functionality in
> > Combine
> > > available "for free" to users of Count.globally(). Whereas new
> > > functionality on Combine would otherwise mandate that *all wrappers*
> > change
> > > to actually be exposed.
> > >
> > > * There's almost no data here, but: we have added new functionality to
> > > Combine (withSideInputs) and have not added new functionality to Count.
> > >
> > >
> > > > Pros:
> > > > - Style consistency with other transforms. Almost all transforms have
> > > their
> > > > own class, and their factory functions return that class.
> > > >
> > >
> > > IMO this should only happen if the user needs that class. For all
> > examples
> > > I'm aware of,
> > > the returned class has stuff you need to do, like configuring coders or
> > > side inputs or other parameters.
> > > IMO if the user need not configure, return the least constraining thing
> > you
> > > can.
> > >
> > >
> > > > - Implementation can evolve. However, in case of Count, that is
> > unlikely.
> > > >
> > >
> > > +1 to "unlikely"
> > >
> > >
> > > > - ...Or is it? If the transform has a concrete class, then the runner
> > can
> > > > intercept that class and e.g. provide an (even) more efficient
> > > > implementation of Count.Globally. This gets much more awkward if the
> > > runner
> > > > has to intercept every Combine and check whether it's combining
> using a
> > > > CountFn.
> > > >
> > >
> > > Post-runner API, this argument goes away. Instead, the runner will need
> > to
> > > inspect generically the attributes of the transform to do the
> > replacement.
> > >
> > > Summarizing: I currently am a -0.8 on this proposal.
> > >
> > >
> > > >
> > > > So, I propose to add this as a style guide rule, and solve the
> problem
> > in
> > > > "Cons" by saying "Yeah, if you want the extra features of the
> transform
> > > > you're expanding into, you have to propagate them through the API of
> > your
> > > > transform and delegate to the underlying transform manually".
> > > >
> > > > Thoughts?
> > > >
> > >
> >
>
Re: Should you always have a separate PTransform class for a new transform?
Posted by Kenneth Knowles <kl...@google.com.INVALID>.
I am +0.7 on this idea. My rationale is contained in this thread, but I
thought I would paraphrase it anyhow:
"You automatically get all the features of Combine" / "If you add a feature
to Combine you have to update all wrappers"
0. I have been in some of the mentioned historical discussions and was
swayed enough to not bother trying to change things, but I don't actually
think the abstraction breakage is worth the benefits. I think the status
quo is hanging on because it is the status quo.
1. Any Combine wrapper should also be providing its CombineFn, for use with
state. So the user already has a good way to get the same result without
the wrapper, e.g. Combine.globally(Mean.of()). There are reasons to provide
a factory method, such as abstracting away tweaking of defaults, but I
don't know of any cases of this happening.
2. Most interesting transforms will not just be Combine wrappers - we have
a lot of them because we try to write pithy examples so I bet this issue is
a bigger deal to us than to our users. I don't have data to back this up;
maybe users do write a bunch of Combine wrappers, but IMO they should spend
their time elsewhere. I certainly would. So the burden of people updating
their Combine wrappers is not compelling to me.
2a. Consider also Count.perElement(). It really is a glorified combine, but
none of the supposed benefits of automatic propagation of tuning knobs can
by accrue to it, because it is not _just_ a Combine.
3. Those transforms that really are just Combine wrappers will not require
update when there is a new feature added to Combine. A new performance
tuning knob, perhaps, but as Eugene points out, new features like side
inputs or access to pipeline options aren't automatically applicable even
if your transform is a glorified CombineFn.
3a. And since the argument is only about universally applicable builder
methods on Combine, how many more do you imagine we will be adding?
3b. You can use type hackery to ensure you don't forget a knob, along the
lines of `CountPerElement implements HasACombineInsideIt<CountPerElement>`.
This would actually be a step towards enabling e.g. hot key counting in
Count.PerElement
4. If a transform that is just a wrapper needs to evolve into more, then
you have to make a backwards incompatible change because you broke your
abstractions, and you incur all the manual programming cost anyhow.
If I were to write a style guide, I might summarize as:
(a) If you are writing an interesting CombineFn, give it some public
factory methods.
(b) If you are writing a transform that _by definition, forever and always_
is a wrapper on Combine, stop after (a)
(c) If you are writing a transform that has conceivably multiple
implementation choices, you need an abstraction boundary to protect against
the current decision.
(d) Only return a class more specific than PTransform if your transform has
more-specific methods to call.
All that said, I'm not sure how much influence this particular collection
of guidelines will have, hence I don't feel a full +1. (except (a) which is
very important).
Kenn
On Tue, Feb 7, 2017 at 5:59 PM, Eugene Kirpichov <
kirpichov@google.com.invalid> wrote:
> There's 2 points here
>
> 1. If Count.globally() is implemented via Combine.globally(), then should
> Count.globally() return a Combine.Globally, or should it wrap it into a new
> class Count.Globally? (that's what I'm wondering in this thread)
>
> I think the "least visibility" argument here would lead us to saying we
> should wrap into a new class, because returning a Combine.Globally leaks
> the implementation detail that Count is always implemented via Combine.
>
> 2. If Thumbs.twiddle() transforms Foo to Bar, should it return a
> PTransform<Foo, Bar>, or should it return Thumbs.Twiddle (which extends
> PTransform<Foo, Bar>)? This is where your "least visibility" argument from
> that thread applies more - we're exposing a more specific type - but I
> think by exposing this type we are not imposing any additional obligations
> on ourselves and not leaking any details: saying "twiddle() returns a
> Twiddle" is as good as saying nothing.
>
> In case of Combine, the ability to additionally configure it for the user
> is a strong argument. But I think we've been through a similar situation
> and made a different decision: bounded reads from unbounded sources. We
> started off with exposing UnboundedSource because a user might want to
> specify .withMaxNumRecords() and .withMaxReadTime() on it, but changed our
> minds to saying "if you're developing an unbounded connector, and if you
> want to make a promise that this is part of your connector's public API,
> then you have to implement these functions yourself". The reasoning was
> that it might not stay an UnboundedSource forever - and indeed, with SDF,
> it won't, and the implementation of these functions will become custom.
>
> Also, the choice to return a Count.Globally (and propagate the needed
> functions from Combine.Globally) makes the assumption that as a developer
> of the Count.Globally transform, you fully control its API surface. E.g. by
> supporting a .withFanout() function, you're saying "When counting things
> globally, you may need to configure the fanout". Corollary: you also
> control what functions its API *doesn't* have - e.g. it probably shouldn't
> have .withSideInputs() because side inputs make no sense here.
>
> The choice to return a Combine.Globally lets you automatically get all new
> APIs that Combine.Globally may ever get - which may or may not be a good
> thing. It is good because these functions may be useful; it is bad because
> as a developer of Count.Globally it is now your responsibility to
> anticipate all possible interactions of parameters users might set on it
> with the particular CombineFn you supplied; and it is your responsibility
> to keep that up-to-date as Combine.Globally evolves (e.g. it might some day
> add a parameter that interacts badly with CountFn and then you probably
> should warn users not to set it). Granted, in this particular case it's
> easy, but we're talking about the general principle.
>
> On Tue, Feb 7, 2017 at 5:45 PM Dan Halperin <dh...@google.com.invalid>
> wrote:
>
> > A little bit more inline:
> >
> > On Tue, Feb 7, 2017 at 5:15 PM, Eugene Kirpichov <
> > kirpichov@google.com.invalid> wrote:
> >
> > > Hello,
> > >
> > > I was auditing Beam for violations of PTransform style guide
> > > https://beam.apache.org/contribute/ptransform-style-guide/ and came
> > across
> > > another style point that deserves discussion.
> > >
> > > Look at Count transform:
> > >
> > > public static <T> Combine.Globally<T, Long> globally() {
> > > return Combine.globally(new CountFn<T>());
> > > }
> > >
> > > public static <K, V> Combine.PerKey<K, V, Long> perKey() {
> > > return Combine.<K, V, Long>perKey(new CountFn<V>());
> > > }
> > >
> > > public static <T> PerElement<T> perElement() {
> > > return new PerElement<>();
> > > }
> > >
> > > I asked myself: should globally() and perKey() also define wrapper
> > classes
> > > - e.g. should it be "public static <T> Globally<T, Long> globally()"
> > where
> > > "Globally" is a new inner class of Count?
> > >
> > > I argue that the answer is yes, but it's not clear-cut.
> > > Cons:
> > > - If we return a Combine.Globally, the user can use the features
> provided
> > > by Combine.Globally - e.g. .withDefaults(), .withFanout(),
> > > .asSingletonView().
> > >
> >
> > +1 to this point, which was a conscious decision in the pre-Beam days
> > (which of course means it IS worth revisiting ;).
> > Trying to replay the reasoning:
> >
> > * If wrapping, the author of a new Count.Globally can now only make the
> > extra functionality in Combine available by similarly exposing all such
> > functions.
> >
> > * Conversely, the current implementation makes new functionality in
> Combine
> > available "for free" to users of Count.globally(). Whereas new
> > functionality on Combine would otherwise mandate that *all wrappers*
> change
> > to actually be exposed.
> >
> > * There's almost no data here, but: we have added new functionality to
> > Combine (withSideInputs) and have not added new functionality to Count.
> >
> >
> > > Pros:
> > > - Style consistency with other transforms. Almost all transforms have
> > their
> > > own class, and their factory functions return that class.
> > >
> >
> > IMO this should only happen if the user needs that class. For all
> examples
> > I'm aware of,
> > the returned class has stuff you need to do, like configuring coders or
> > side inputs or other parameters.
> > IMO if the user need not configure, return the least constraining thing
> you
> > can.
> >
> >
> > > - Implementation can evolve. However, in case of Count, that is
> unlikely.
> > >
> >
> > +1 to "unlikely"
> >
> >
> > > - ...Or is it? If the transform has a concrete class, then the runner
> can
> > > intercept that class and e.g. provide an (even) more efficient
> > > implementation of Count.Globally. This gets much more awkward if the
> > runner
> > > has to intercept every Combine and check whether it's combining using a
> > > CountFn.
> > >
> >
> > Post-runner API, this argument goes away. Instead, the runner will need
> to
> > inspect generically the attributes of the transform to do the
> replacement.
> >
> > Summarizing: I currently am a -0.8 on this proposal.
> >
> >
> > >
> > > So, I propose to add this as a style guide rule, and solve the problem
> in
> > > "Cons" by saying "Yeah, if you want the extra features of the transform
> > > you're expanding into, you have to propagate them through the API of
> your
> > > transform and delegate to the underlying transform manually".
> > >
> > > Thoughts?
> > >
> >
>
Re: Should you always have a separate PTransform class for a new transform?
Posted by Eugene Kirpichov <ki...@google.com.INVALID>.
There's 2 points here
1. If Count.globally() is implemented via Combine.globally(), then should
Count.globally() return a Combine.Globally, or should it wrap it into a new
class Count.Globally? (that's what I'm wondering in this thread)
I think the "least visibility" argument here would lead us to saying we
should wrap into a new class, because returning a Combine.Globally leaks
the implementation detail that Count is always implemented via Combine.
2. If Thumbs.twiddle() transforms Foo to Bar, should it return a
PTransform<Foo, Bar>, or should it return Thumbs.Twiddle (which extends
PTransform<Foo, Bar>)? This is where your "least visibility" argument from
that thread applies more - we're exposing a more specific type - but I
think by exposing this type we are not imposing any additional obligations
on ourselves and not leaking any details: saying "twiddle() returns a
Twiddle" is as good as saying nothing.
In case of Combine, the ability to additionally configure it for the user
is a strong argument. But I think we've been through a similar situation
and made a different decision: bounded reads from unbounded sources. We
started off with exposing UnboundedSource because a user might want to
specify .withMaxNumRecords() and .withMaxReadTime() on it, but changed our
minds to saying "if you're developing an unbounded connector, and if you
want to make a promise that this is part of your connector's public API,
then you have to implement these functions yourself". The reasoning was
that it might not stay an UnboundedSource forever - and indeed, with SDF,
it won't, and the implementation of these functions will become custom.
Also, the choice to return a Count.Globally (and propagate the needed
functions from Combine.Globally) makes the assumption that as a developer
of the Count.Globally transform, you fully control its API surface. E.g. by
supporting a .withFanout() function, you're saying "When counting things
globally, you may need to configure the fanout". Corollary: you also
control what functions its API *doesn't* have - e.g. it probably shouldn't
have .withSideInputs() because side inputs make no sense here.
The choice to return a Combine.Globally lets you automatically get all new
APIs that Combine.Globally may ever get - which may or may not be a good
thing. It is good because these functions may be useful; it is bad because
as a developer of Count.Globally it is now your responsibility to
anticipate all possible interactions of parameters users might set on it
with the particular CombineFn you supplied; and it is your responsibility
to keep that up-to-date as Combine.Globally evolves (e.g. it might some day
add a parameter that interacts badly with CountFn and then you probably
should warn users not to set it). Granted, in this particular case it's
easy, but we're talking about the general principle.
On Tue, Feb 7, 2017 at 5:45 PM Dan Halperin <dh...@google.com.invalid>
wrote:
> A little bit more inline:
>
> On Tue, Feb 7, 2017 at 5:15 PM, Eugene Kirpichov <
> kirpichov@google.com.invalid> wrote:
>
> > Hello,
> >
> > I was auditing Beam for violations of PTransform style guide
> > https://beam.apache.org/contribute/ptransform-style-guide/ and came
> across
> > another style point that deserves discussion.
> >
> > Look at Count transform:
> >
> > public static <T> Combine.Globally<T, Long> globally() {
> > return Combine.globally(new CountFn<T>());
> > }
> >
> > public static <K, V> Combine.PerKey<K, V, Long> perKey() {
> > return Combine.<K, V, Long>perKey(new CountFn<V>());
> > }
> >
> > public static <T> PerElement<T> perElement() {
> > return new PerElement<>();
> > }
> >
> > I asked myself: should globally() and perKey() also define wrapper
> classes
> > - e.g. should it be "public static <T> Globally<T, Long> globally()"
> where
> > "Globally" is a new inner class of Count?
> >
> > I argue that the answer is yes, but it's not clear-cut.
> > Cons:
> > - If we return a Combine.Globally, the user can use the features provided
> > by Combine.Globally - e.g. .withDefaults(), .withFanout(),
> > .asSingletonView().
> >
>
> +1 to this point, which was a conscious decision in the pre-Beam days
> (which of course means it IS worth revisiting ;).
> Trying to replay the reasoning:
>
> * If wrapping, the author of a new Count.Globally can now only make the
> extra functionality in Combine available by similarly exposing all such
> functions.
>
> * Conversely, the current implementation makes new functionality in Combine
> available "for free" to users of Count.globally(). Whereas new
> functionality on Combine would otherwise mandate that *all wrappers* change
> to actually be exposed.
>
> * There's almost no data here, but: we have added new functionality to
> Combine (withSideInputs) and have not added new functionality to Count.
>
>
> > Pros:
> > - Style consistency with other transforms. Almost all transforms have
> their
> > own class, and their factory functions return that class.
> >
>
> IMO this should only happen if the user needs that class. For all examples
> I'm aware of,
> the returned class has stuff you need to do, like configuring coders or
> side inputs or other parameters.
> IMO if the user need not configure, return the least constraining thing you
> can.
>
>
> > - Implementation can evolve. However, in case of Count, that is unlikely.
> >
>
> +1 to "unlikely"
>
>
> > - ...Or is it? If the transform has a concrete class, then the runner can
> > intercept that class and e.g. provide an (even) more efficient
> > implementation of Count.Globally. This gets much more awkward if the
> runner
> > has to intercept every Combine and check whether it's combining using a
> > CountFn.
> >
>
> Post-runner API, this argument goes away. Instead, the runner will need to
> inspect generically the attributes of the transform to do the replacement.
>
> Summarizing: I currently am a -0.8 on this proposal.
>
>
> >
> > So, I propose to add this as a style guide rule, and solve the problem in
> > "Cons" by saying "Yeah, if you want the extra features of the transform
> > you're expanding into, you have to propagate them through the API of your
> > transform and delegate to the underlying transform manually".
> >
> > Thoughts?
> >
>
Re: Should you always have a separate PTransform class for a new transform?
Posted by Dan Halperin <dh...@google.com.INVALID>.
A little bit more inline:
On Tue, Feb 7, 2017 at 5:15 PM, Eugene Kirpichov <
kirpichov@google.com.invalid> wrote:
> Hello,
>
> I was auditing Beam for violations of PTransform style guide
> https://beam.apache.org/contribute/ptransform-style-guide/ and came across
> another style point that deserves discussion.
>
> Look at Count transform:
>
> public static <T> Combine.Globally<T, Long> globally() {
> return Combine.globally(new CountFn<T>());
> }
>
> public static <K, V> Combine.PerKey<K, V, Long> perKey() {
> return Combine.<K, V, Long>perKey(new CountFn<V>());
> }
>
> public static <T> PerElement<T> perElement() {
> return new PerElement<>();
> }
>
> I asked myself: should globally() and perKey() also define wrapper classes
> - e.g. should it be "public static <T> Globally<T, Long> globally()" where
> "Globally" is a new inner class of Count?
>
> I argue that the answer is yes, but it's not clear-cut.
> Cons:
> - If we return a Combine.Globally, the user can use the features provided
> by Combine.Globally - e.g. .withDefaults(), .withFanout(),
> .asSingletonView().
>
+1 to this point, which was a conscious decision in the pre-Beam days
(which of course means it IS worth revisiting ;).
Trying to replay the reasoning:
* If wrapping, the author of a new Count.Globally can now only make the
extra functionality in Combine available by similarly exposing all such
functions.
* Conversely, the current implementation makes new functionality in Combine
available "for free" to users of Count.globally(). Whereas new
functionality on Combine would otherwise mandate that *all wrappers* change
to actually be exposed.
* There's almost no data here, but: we have added new functionality to
Combine (withSideInputs) and have not added new functionality to Count.
> Pros:
> - Style consistency with other transforms. Almost all transforms have their
> own class, and their factory functions return that class.
>
IMO this should only happen if the user needs that class. For all examples
I'm aware of,
the returned class has stuff you need to do, like configuring coders or
side inputs or other parameters.
IMO if the user need not configure, return the least constraining thing you
can.
> - Implementation can evolve. However, in case of Count, that is unlikely.
>
+1 to "unlikely"
> - ...Or is it? If the transform has a concrete class, then the runner can
> intercept that class and e.g. provide an (even) more efficient
> implementation of Count.Globally. This gets much more awkward if the runner
> has to intercept every Combine and check whether it's combining using a
> CountFn.
>
Post-runner API, this argument goes away. Instead, the runner will need to
inspect generically the attributes of the transform to do the replacement.
Summarizing: I currently am a -0.8 on this proposal.
>
> So, I propose to add this as a style guide rule, and solve the problem in
> "Cons" by saying "Yeah, if you want the extra features of the transform
> you're expanding into, you have to propagate them through the API of your
> transform and delegate to the underlying transform manually".
>
> Thoughts?
>