Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassCastException when trying to get an element from IVpcNetwork.getPrivateSubnets() #1981

Closed
foscraig opened this issue Mar 8, 2019 · 10 comments · Fixed by aws/jsii#406
Closed
Assignees
Labels
bug This issue is a bug. language/java Related to Java bindings

Comments

@foscraig
Copy link

foscraig commented Mar 8, 2019

When trying to get the first private subnet in a IVpcNetwork, it returns a ClassCastException.
The relevant code:

  public static HashMap<String, String> getVpcInfo(IVpcNetwork vpc) {
    String vpcId = vpc.getVpcId();
    IVpcSubnet subnet = vpc.getPrivateSubnets().get(0);         // line 39
    HashMap<String, String> vpcInfo = new HashMap<>();
    vpcInfo.put("vpcId", vpcId);
    vpcInfo.put("subnetId", subnet.getSubnetId());
    vpcInfo.put("securityGroupId", getSecurityGroupId(vpcId));
    return vpcInfo;
  }

Runtime exception (cdk deploy):

Exception in thread "main" java.lang.ClassCastException: software.amazon.awscdk.Construct cannot be cast to software.amazon.awscdk.services.ec2.IVpcSubnet
        at com.amazonaws.emr.releasetool.infrastructure.VpcResourceUtilities.getVpcInfo(VpcResourceUtilities.java:39)
        at com.amazonaws.emr.releasetool.BigTopRpmBuildStack.build(BigTopRpmBuildStack.java:202)
        at com.amazonaws.emr.releasetool.ReleaseBuildApp.main(ReleaseBuildApp.java:47)
Subprocess exited with error 1
@foscraig foscraig changed the title ClassCastException when trying to get an element from VpcNetwork.getPrivateSubnets() ClassCastException when trying to get an element from IVpcNetwork.getPrivateSubnets() Mar 8, 2019
@eladb
Copy link
Contributor

eladb commented Mar 12, 2019

I am unable to reproduce this on the latest release. What version are you using?

I believe this has been resolved by aws/jsii#321

@eladb eladb added needs-response bug This issue is a bug. language/java Related to Java bindings labels Mar 12, 2019
@foscraig
Copy link
Author

foscraig commented Mar 12, 2019

Could it be that I need to upgrade the toolkit? I upgraded my Maven dependencies but I'm realizing that I might need to upgrade the toolkit itself. That would definitely be a good thing to put in documentation if so because I don't know how right now. (I tried npm upgrade -g cdk.) I'm using 0.25.2 as a Maven dependency and cdk --version 0.20.0 (build cbeea75)

@foscraig
Copy link
Author

Ok, I was using the wrong command. The upgrade is just npm install -g aws-cdk. Sorry, my bad. I do however still feel this should be emphasized in the docs.

@foscraig
Copy link
Author

Sadly, upgrading did not resolve this. I am now using 0.25.3 and I get the same exception at runtime (using most up-to-date toolkit of same version).

@foscraig
Copy link
Author

Similarly, when I've tried to use the following as a workaround, I still get the ClassCastException when trying to operate on this List of IVpcSubnets.

  private void createMountTarget(Stack stack, String fileSystemId) {
    VpcNetworkProviderProps vpcProps = VpcNetworkProviderProps.builder()
        .withVpcName(CODEBUILD_VPC_NAME)
        .build();
    IVpcNetwork vpc = VpcNetwork.importFromContext(stack, "Imported VPC", vpcProps);
    List<IVpcSubnet> subnets = vpc.subnets();
    IVpcSubnet privateSubnet = null;
    for (IVpcSubnet sub:subnets) {     \\ line 52
      if (sub.getNode().getAspects().contains(CODEBUILD_VPC_PRIVATE_SUBNET1)) {
        privateSubnet = sub;
      }
    }
    CfnMountTargetProps mountTargetProps = CfnMountTargetProps.builder()
        .withSubnetId(privateSubnet.getSubnetId())
        .withFileSystemId(fileSystemId)
        .build();
    CfnMountTarget mountTarget = new CfnMountTarget(stack, "User EFS MountTarget", mountTargetProps);
  }

returns this error:

java.lang.ClassCastException: software.amazon.awscdk.Construct cannot be cast to software.amazon.awscdk.services.ec2.IVpcSubnet
        at com.amazonaws.emr.releasetool.infrastructure.EfsResource.createMountTarget(EfsResource.java:52)
        at com.amazonaws.emr.releasetool.infrastructure.EfsResource.createEFS(EfsResource.java:39)
        at com.amazonaws.emr.releasetool.ReleaseBuildApp.createUserInfraStructure(ReleaseBuildApp.java:89)
        at com.amazonaws.emr.releasetool.ReleaseBuildApp.main(ReleaseBuildApp.java:39)
Exception in thread "main" java.lang.NullPointerException
        at com.amazonaws.emr.releasetool.BigTopRpm.createBuildStage(BigTopRpm.java:216)
        at com.amazonaws.emr.releasetool.BigTopRpm.<init>(BigTopRpm.java:106)
        at com.amazonaws.emr.releasetool.BigTopRpmBuilder.build(BigTopRpmBuilder.java:82)
        at com.amazonaws.emr.releasetool.ReleaseBuildApp.main(ReleaseBuildApp.java:63)

Without a workaround this blocks me from deploying.

@rix0rrr
Copy link
Contributor

rix0rrr commented Mar 21, 2019

I bet this error is around returning the right type by the JSII kernel.

@rix0rrr
Copy link
Contributor

rix0rrr commented Mar 21, 2019

Yes, fairly sure, since the actual class type is going to be ImportedVpcSubnet, and I recall not agreeing with the latest change that went into the selection of the type to return in that case.

@rix0rrr
Copy link
Contributor

rix0rrr commented Mar 21, 2019

Hmm that does not seem to be it.

Could you run with the environment variable JSII_DEBUG=1 and paste the last 100 or so lines of the trace?

@rix0rrr
Copy link
Contributor

rix0rrr commented Mar 21, 2019

Ah no, it IS that. It's just that this pull request: aws/jsii#346 just didn't fix the case where the return type is an array.

@foscraig
Copy link
Author

Posting the element of the array (or for loop maybe? I guess) and after.

After that it seems to show it returning an array of Constructs.

[jsii-kernel] fromSandbox ImportedVpcSubnet {
  dependencyRoots: [ [Circular] ],
  node:
   ConstructNode {
     host: [Circular],
     aspects: [],
     _children: {},
     context: {},
     _metadata: [],
     references: Set {},
     dependencies: Set {},
     _locked: false,
     invokedAspects: [],
     id: 'PrivateSubnet4',
     scope:
      ImportedVpcNetwork {
        dependencyRoots: [Array],
        node: [ConstructNode],
        internetDependencies: [],
        natDependencies: [],
        props: [Object],
        vpcId: 'vpc-072e7c4e36bed379d',
        availabilityZones: [Array],
        vpnGatewayId: 'vgw-03788463cdcfa4e95',
        publicSubnets: [Array],
        privateSubnets: [Array],
        isolatedSubnets: [] } },
  props:
   { availabilityZone: 'us-west-2d',
     subnetId: 'subnet-068c09755c58d2c20' },
  internetConnectivityEstablished: ConcreteDependable { _dependencyRoots: [] },
  subnetId: 'subnet-068c09755c58d2c20',
  availabilityZone: 'us-west-2d' } undefined
[jsii-kernel] creating objref for ImportedVpcSubnet {
  dependencyRoots: [ [Circular] ],
  node:
   ConstructNode {
     host: [Circular],
     aspects: [],
     _children: {},
     context: {},
     _metadata: [],
     references: Set {},
     dependencies: Set {},
     _locked: false,
     invokedAspects: [],
     id: 'PrivateSubnet4',
     scope:
      ImportedVpcNetwork {
        dependencyRoots: [Array],
        node: [ConstructNode],
        internetDependencies: [],
        natDependencies: [],
        props: [Object],
        vpcId: 'vpc-072e7c4e36bed379d',
        availabilityZones: [Array],
        vpnGatewayId: 'vgw-03788463cdcfa4e95',
        publicSubnets: [Array],
        privateSubnets: [Array],
        isolatedSubnets: [] } },
  props:
   { availabilityZone: 'us-west-2d',
     subnetId: 'subnet-068c09755c58d2c20' },
  internetConnectivityEstablished: ConcreteDependable { _dependencyRoots: [] },
  subnetId: 'subnet-068c09755c58d2c20',
  availabilityZone: 'us-west-2d' }
< {"ok":{"result":[{"$jsii.byref":"@aws-cdk/cdk.Construct@10081"},{"$jsii.byref":"@aws-cdk/cdk.Construct@10082"},{"$jsii.byref":"@aws-cdk/cdk.Construct@10083"},{"$jsii.byref":"@aws-cdk/cdk.Construct@10084"}]}}
> {"api":"naming","assembly":"@aws-cdk/cdk"}
[jsii-kernel] naming @aws-cdk/cdk
< {"ok":{"naming":{"dotnet":{"assemblyOriginatorKeyFile":"../../key.snk","namespace":"Amazon.CDK","packageId":"Amazon.CDK","signAssembly":true},"java":{"maven":{"artifactId":"cdk","groupId":"software.amazon.awscdk"},"package":"software.amazon.awscdk"},"js":{"npm":"@aws-cdk/cdk"}}}}
> {"api":"naming","assembly":"@aws-cdk/cdk"}
[jsii-kernel] naming @aws-cdk/cdk
< {"ok":{"naming":{"dotnet":{"assemblyOriginatorKeyFile":"../../key.snk","namespace":"Amazon.CDK","packageId":"Amazon.CDK","signAssembly":true},"java":{"maven":{"artifactId":"cdk","groupId":"software.amazon.awscdk"},"package":"software.amazon.awscdk"},"js":{"npm":"@aws-cdk/cdk"}}}}
> {"api":"naming","assembly":"@aws-cdk/cdk"}
[jsii-kernel] naming @aws-cdk/cdk
< {"ok":{"naming":{"dotnet":{"assemblyOriginatorKeyFile":"../../key.snk","namespace":"Amazon.CDK","packageId":"Amazon.CDK","signAssembly":true},"java":{"maven":{"artifactId":"cdk","groupId":"software.amazon.awscdk"},"package":"software.amazon.awscdk"},"js":{"npm":"@aws-cdk/cdk"}}}}
> {"api":"naming","assembly":"@aws-cdk/cdk"}
[jsii-kernel] naming @aws-cdk/cdk
< {"ok":{"naming":{"dotnet":{"assemblyOriginatorKeyFile":"../../key.snk","namespace":"Amazon.CDK","packageId":"Amazon.CDK","signAssembly":true},"java":{"maven":{"artifactId":"cdk","groupId":"software.amazon.awscdk"},"package":"software.amazon.awscdk"},"js":{"npm":"@aws-cdk/cdk"}}}}
Exception in thread "main" java.lang.ClassCastException: software.amazon.awscdk.Construct cannot be cast to software.amazon.awscdk.services.ec2.IVpcSubnet
        at com.amazonaws.emr.releasetool.infrastructure.EfsResource.createMountTarget(EfsResource.java:57)
        at com.amazonaws.emr.releasetool.infrastructure.EfsResource.<init>(EfsResource.java:39)
        at com.amazonaws.emr.releasetool.ReleaseBuildApp.createUserInfraStructure(ReleaseBuildApp.java:108)
        at com.amazonaws.emr.releasetool.ReleaseBuildApp.main(ReleaseBuildApp.java:47)
Subprocess exited with error 1

rix0rrr pushed a commit to aws/jsii that referenced this issue Mar 22, 2019
Serialize and deserialize types according to their declared static type,
and add validation on the runtime types matching the declared types.

This is in contrast to previously, when we mostly only used the runtime
types to determine what to do, and harly any validation was done. The
runtime types used to be able to freely disagree with the declared
types, and we put a lot of burden on the JSII runtimes at the other
end of the wire.

Fix tests that used to exercise the API with invalid arguments.

Fixes aws/aws-cdk#1981.
@rix0rrr rix0rrr self-assigned this Mar 25, 2019
rix0rrr pushed a commit to aws/jsii that referenced this issue Mar 26, 2019
Serialize and deserialize types according to their declared static type,
and add validation on the runtime types matching the declared types.

This is in contrast to previously, when we mostly only used the runtime
types to determine what to do, and harly any validation was done. The
runtime types used to be able to freely disagree with the declared
types, and we put a lot of burden on the JSII runtimes at the other
end of the wire.

Fix tests that used to exercise the API with invalid arguments.

Remove Proxy objects since they only existed to prevent accidentally
overwriting certain keys via the jsii interface, and Symbols serve
the same purpose (but simpler).

Fixes aws/aws-cdk#1981.
rix0rrr added a commit to aws/jsii that referenced this issue Mar 28, 2019
Serialize and deserialize types according to their declared static type,
and add validation on the runtime types matching the declared types.

This is in contrast to previously, when we mostly only used the runtime
types to determine what to do, and harly any validation was done. The
runtime types used to be able to freely disagree with the declared
types, and we put a lot of burden on the JSII runtimes at the other
end of the wire.

Fix tests that used to exercise the API with invalid arguments.

Remove Proxy objects since they only existed to prevent accidentally
overwriting certain keys via the jsii interface, and Symbols serve
the same purpose (but simpler).

Fixes aws/aws-cdk#1981.
RomainMuller added a commit to aws/jsii that referenced this issue Mar 28, 2019
### Bug Fixes

* **kernel:** make type serialization explicit and recursive ([#401](#401)) ([0a83d52](0a83d52)), closes [aws/aws-cdk#1981](aws/aws-cdk#1981)
* **runtime:** Passing 'this' to a callback from constructor ([#395](#395)) ([850f42b](850f42b))
RomainMuller added a commit to aws/jsii that referenced this issue Mar 28, 2019
### Bug Fixes

* **kernel:** make type serialization explicit and recursive ([#401](#401)) ([0a83d52](0a83d52)), closes [aws/aws-cdk#1981](aws/aws-cdk#1981)
* **runtime:** Passing 'this' to a callback from constructor ([#395](#395)) ([850f42b](850f42b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. language/java Related to Java bindings
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants